Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active September 26, 2022 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trikitrok/d26432d888aef427016d8bc5fed58ab3 to your computer and use it in GitHub Desktop.
Save trikitrok/d26432d888aef427016d8bc5fed58ab3 to your computer and use it in GitHub Desktop.
import {UnusualSpendingDetector} from "../src/UnusualSpendingDetector";
import {when} from 'jest-when'
describe('UnusualSpendingDetector', () => {
const currentMonth = '2020-02';
const previousMonth = '2020-01';
const userId = '1234';
let paymentsRepository;
let detector;
beforeEach(() => {
paymentsRepository = {
find: jest.fn()
};
const calendar = {
getCurrentMonth: () => currentMonth,
getPreviousMonth: () => previousMonth,
};
detector = new UnusualSpendingDetector(paymentsRepository, calendar);
}
);
test('detects no unusual spending when there are no payments', () => {
when(paymentsRepository.find).calledWith(userId, currentMonth).mockReturnValue([]);
when(paymentsRepository.find).calledWith(userId, previousMonth).mockReturnValue([]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([]);
});
test('detects an unusual spending when spending for a category in consecutive months grew 50% or more', () => {
when(paymentsRepository.find).calledWith(userId, currentMonth)
.mockReturnValue([payment('food', 200, currentMonth)]);
when(paymentsRepository.find).calledWith(userId, previousMonth)
.mockReturnValue([payment('food', 100, previousMonth)]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([unusualSpending('food', 200)]);
});
test('detects no unusual spending when there are no payments for a category in current month', () => {
when(paymentsRepository.find).calledWith(userId, currentMonth).mockReturnValue([]);
when(paymentsRepository.find).calledWith(userId, previousMonth)
.mockReturnValue([payment('food', 200, currentMonth)]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([]);
});
test('detects no unusual spending when there were no payments for a category in previous month', () => {
when(paymentsRepository.find).calledWith(userId, currentMonth)
.mockReturnValue([payment('transport', 200, currentMonth)]);
when(paymentsRepository.find).calledWith(userId, previousMonth)
.mockReturnValue([payment('food', 200, currentMonth)]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([]);
});
test.each([[100, 100], [149.99, 100]])(
'detects no unusual spending when consecutive monthly spending for a category do not increase by 50% or more',
(currentMonthPaymentAmount, previousMonthPaymentAmount) => {
when(paymentsRepository.find).calledWith(userId, currentMonth)
.mockReturnValue([payment('food', currentMonthPaymentAmount, currentMonth)]);
when(paymentsRepository.find).calledWith(userId, previousMonth)
.mockReturnValue([payment('food', previousMonthPaymentAmount, previousMonth)]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([]);
});
test(
'detects an unusual spending when spending for a category having multiple payments grew 50% or more in current month',
() => {
when(paymentsRepository.find).calledWith(userId, currentMonth)
.mockReturnValue([
payment('food', 100, currentMonth),
payment('food', 100, currentMonth)
]);
when(paymentsRepository.find).calledWith(userId, previousMonth)
.mockReturnValue([payment('food', 100, previousMonth)]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([unusualSpending('food', 200)]);
});
test(
'detects no unusual spending when spending for a category does not grow over 50% in current month wit multiple payments ',
() => {
when(paymentsRepository.find).calledWith(userId, currentMonth)
.mockReturnValue([payment('food', 150, previousMonth)]);
when(paymentsRepository.find).calledWith(userId, previousMonth)
.mockReturnValue([
payment('food', 100, currentMonth),
payment('food', 100, currentMonth)
]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([]);
});
test('detects unusual spendings for multiple categories', () => {
when(paymentsRepository.find).calledWith(userId, currentMonth)
.mockReturnValue([
payment('food', 100, previousMonth),
payment('transport', 200, previousMonth)
]);
when(paymentsRepository.find).calledWith(userId, previousMonth)
.mockReturnValue([
payment('food', 100, currentMonth),
payment('transport', 100, currentMonth)
]);
const unusualSpendings = detector.detect(userId);
expect(unusualSpendings).toEqual([unusualSpending('transport', 200)]);
});
function payment(category, amount, month) {
return {category, amount, month};
}
function unusualSpending(category, amount) {
return {category, amount};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment