Skip to content

Commit

Permalink
Update and test mitochondria impl. for recurring
Browse files Browse the repository at this point in the history
Skipped testing /byDate and /byDateRange for recurring due to its
implementation details being under discussion. Ref. Kodeworks/liquidator-backend#52
  • Loading branch information
FredrikAugust committed Aug 5, 2019
1 parent 2da4bac commit fee1bb6
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/declarations/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface IRecurringTransactionTemplate {
money: number;
type: TransactionType;
description: string;
notes: string;
notes?: string;
}

export type RecurringTransactionInterval = 'DA' | 'MO';
Expand Down
30 changes: 30 additions & 0 deletions src/helpers/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, RenderOptions } from '@testing-library/react';
import moment from 'moment';
import { ICompany } from '../declarations/company';
import { IUser } from '../declarations/user';
import * as api from '../mitochondria';
Expand Down Expand Up @@ -83,3 +84,32 @@ export const createTx = async (companyId: number) => {
() => Promise<true>
];
};

export const createRecurringTx = async (companyId: number) => {
const transaction = await api.createRecurringTransaction({
company_id: companyId,
end_date: moment()
.year(2020)
.format('YYYY-MM-DD'),
interval: 3,
interval_type: 'MO',
start_date: moment().format('YYYY-MM-DD'),
template: {
description: 'Bursdagspenger',
money: 20000,
type: 'IN',
},
});

return [
transaction,
async () =>
await api.deleteRecurringTransaction(
transaction.company_id,
transaction.id
),
] as [
import('../declarations/transaction').IRecurringTransaction,
() => Promise<true>
];
};
109 changes: 107 additions & 2 deletions src/mitochondria/__test__/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { cleanup } from '@testing-library/react';
import moment from 'moment';
import * as api from '..';
import { ICompany } from '../../declarations/company';
import { ITransaction } from '../../declarations/transaction';
import { createTx, loginDetails, setupTests } from '../../helpers/test-utils';
import {
IRecurringTransaction,
ITransaction,
} from '../../declarations/transaction';
import {
createRecurringTx,
createTx,
loginDetails,
setupTests,
} from '../../helpers/test-utils';

afterEach(cleanup);

Expand Down Expand Up @@ -158,3 +167,99 @@ describe('actions returning plural transactions', () => {
});
});
});

describe('recurring transactions', () => {
test('create a new one', async () => {
const [rtx, clean] = await createRecurringTx(company.id);

expect(rtx).not.toBeFalsy();

await clean();
});

test('delete one', async () => {
const [rtx, clean] = await createRecurringTx(company.id);

await clean();

expect(
api.getRecurringTransactionById(rtx.company_id, rtx.id)
).rejects.toThrow();
});

test('update', async () => {
const [rtx, clean] = await createRecurringTx(company.id);

await api.updateRecurringTransaction({
...rtx,
interval: 4,
template: {
...rtx.template,
type: 'EX',
},
});

const newRtx = await api.getRecurringTransactionById(
rtx.company_id,
rtx.id
);

expect(newRtx).toEqual({
...rtx,
interval: 4,
template: {
...rtx.template,
type: 'EX',
},
});

await clean();
});

describe('paginated responses', () => {
let rtx1: IRecurringTransaction;
let clean1: () => Promise<true>;
let rtx2: IRecurringTransaction;
let clean2: () => Promise<true>;

beforeAll(async () => {
const [r1, c1] = await createRecurringTx(company.id);
rtx1 = r1;
clean1 = c1;

const r2 = await api.createRecurringTransaction({
company_id: company.id,
end_date: moment('2017-01-01').format('YYYY-MM-DD'),
interval: 1,
interval_type: 'DA',
start_date: moment('1999-01-01').format('YYYY-MM-DD'),
template: { money: 1, type: 'EX', description: "C'est la vie" },
});

rtx2 = r2;
clean2 = () => api.deleteRecurringTransaction(r2.company_id, r2.id);
});

afterAll(async () => {
await clean1();
await clean2();
});

test('get all active', async () => {
const resp = await api.getActiveRecurringTransactions(company.id);

expect(resp.results.find(e => e.id === rtx1.id)).not.toBeUndefined();
expect(resp.results.find(e => e.id === rtx2.id)).toBeUndefined();
});

test('get all', async () => {
const resp = await api.getAllRecurringTransactions(company.id);

expect(resp.results.find(e => e.id === rtx1.id)).not.toBeUndefined();
expect(resp.results.find(e => e.id === rtx2.id)).not.toBeUndefined();
});

test.todo('get by date');
test.todo('get by date range');
});
});

0 comments on commit fee1bb6

Please sign in to comment.