/
save.unit.test.js
47 lines (41 loc) · 1.59 KB
/
save.unit.test.js
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
const fs = require('fs');
const Domain = require('server/utilities/domain');
const save = require('server/utilities/save');
const today = require('moment')().format('YYYY-MM-DD');
describe('utilities.save', () => {
describe('saveDomain', () => {
const readFileMock = jest.spyOn(fs, 'readFile');
const appendFileMock = jest.spyOn(fs, 'appendFile');
const domain = Domain.from({ url: 'https://example.com/vulnerable.txt' });
beforeEach(() => {
readFileMock.mockImplementation(
(_file, ...args) => args[args.length - 1](null, 'https://example.com/vulnerable.txt')
);
appendFileMock.mockImplementation((file, data, ...args) => {
args[args.length - 1](null, data.byteLength, data)
});
});
afterEach(() => {
readFileMock.mockRestore();
appendFileMock.mockRestore();
});
it('should save domain to file as it does not currently exist inside urls.txt', () => {
save.saveDomain(domain);
expect(appendFileMock).toHaveBeenCalled();
});
it('should not save domain to file as it currently exist inside urls.txt', () => {
save.saveDomain(domain);
expect(appendFileMock).not.toHaveBeenCalled();
});
});
describe('saveTodaysDate', () => {
it("should save today's date in a text file", () => {
const writeFileSyncMock = jest.spyOn(fs, 'writeFileSync');
writeFileSyncMock.mockImplementation(() => {});
save.saveTodaysDate();
expect(writeFileSyncMock.mock.calls[0][1]).toBe(today);
expect(writeFileSyncMock).toHaveBeenCalled();
writeFileSyncMock.mockRestore();
});
});
})