forked from XeroAPI/xero-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoices.integration.tests.ts
111 lines (83 loc) · 3.96 KB
/
invoices.integration.tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as fs from 'fs';
import * as path from 'path';
import { InvoicesResponse } from '../AccountingAPI-responses';
import { AccountingAPIClient } from '../AccountingAPIClient';
import { getOrCreateInvoiceId } from './helpers/entityId.helpers';
import { getPrivateConfig, setJestTimeout } from './helpers/integration.helpers';
import { createMultipleInvoiceRequest, createSingleInvoiceRequest, createEmailInvoiceRequest } from './request-body/invoice.request.examples';
describe('/invoices', () => {
let xero: AccountingAPIClient;
let invoiceIdsToArchive: string[] = [];
const tmpDownloadFile = path.resolve(__dirname, './temp_result.pdf');
beforeAll(async () => {
setJestTimeout();
const config = getPrivateConfig();
xero = new AccountingAPIClient(config);
});
it('create single', async () => {
const response = await xero.invoices.create(createSingleInvoiceRequest);
collectInvoicesToArchive(response);
expect(response.Invoices.length).toBe(1);
expect(response.Invoices[0].InvoiceID).toBeTruthy();
});
it('create multiple', async () => {
const response = await xero.invoices.create(createMultipleInvoiceRequest);
collectInvoicesToArchive(response);
expect(response.Invoices.length).toBe(createMultipleInvoiceRequest.Invoices.length);
expect(response.Invoices[0].InvoiceID).toBeTruthy();
expect(response.Invoices[1].InvoiceID).toBeTruthy();
});
it('get all', async () => {
const response = await xero.invoices.get();
expect(response).toBeDefined();
expect(response.Id).toBeTruthy();
expect(response.Invoices.length).toBeGreaterThanOrEqual(invoiceIdsToArchive.length);
expect(response.Invoices[0].InvoiceID).toBeTruthy();
});
it('get single', async () => {
const invoiceId = await getOrCreateInvoiceId(xero);
const response = await xero.invoices.get({ InvoiceID: invoiceId });
expect(response).toBeDefined();
expect(response.Id).toBeTruthy();
expect(response.Invoices).toHaveLength(1);
expect(response.Invoices[0].InvoiceID).toBe(invoiceId);
});
it('get single as pdf', async () => {
const response = await xero.invoices.savePDF({ InvoiceID: await getOrCreateInvoiceId(xero), savePath: tmpDownloadFile });
expect(response).toBeUndefined();
const invoiceBuffer = fs.readFileSync(tmpDownloadFile);
expect(invoiceBuffer.byteLength).toBeGreaterThan(3000); // Let's hope all PDFs are bigger than 3000B
});
it('sends invoice email', async () => {
const invoiceResponse = await xero.invoices.create(createEmailInvoiceRequest);
const invoiceId = invoiceResponse.Invoices[0].InvoiceID;
const email = await xero.invoices.email.create({ InvoiceID: invoiceId });
collectInvoicesToArchive(invoiceResponse);
expect(email).toBeNull();
});
// it('get history', async () => {
// const invoiceId = await getOrCreateInvoiceId(xero);
// const response = await xero.invoices.history.get({ InvoiceID: invoiceId });
// expect(response.HistoryRecords[0]).toBeDefined();
// });
describe('Invalid requests', () => {
it('creating an invalid invoice', async () => {
const createInvalidInvoiceRequest = { ...createSingleInvoiceRequest, ...{ Type: 'ImNotARealType' } };
const response = await xero.invoices.create(createInvalidInvoiceRequest);
collectInvoicesToArchive(response);
expect(response.Invoices).toHaveLength(1);
expect(response.Invoices[0].HasErrors).toBeTruthy();
expect(response.Invoices[0].ValidationErrors.length).toBeGreaterThanOrEqual(1);
});
});
afterAll(async () => {
// delete the file
fs.unlinkSync(tmpDownloadFile);
// archive the invoices
const updateRequestBody = invoiceIdsToArchive.map((invoiceId) => ({ InvoiceID: invoiceId, Status: 'DELETED' }));
await xero.invoices.update({ Invoices: updateRequestBody });
});
function collectInvoicesToArchive(response: InvoicesResponse) {
invoiceIdsToArchive = invoiceIdsToArchive.concat(response.Invoices.map((invoice) => invoice.InvoiceID));
}
});