|
1 |
| -const test = require('node:test'); |
| 1 | +const testUtil = require('node:test'); |
2 | 2 | const assert = require('assert');
|
3 | 3 | const fs = require('fs');
|
4 | 4 |
|
5 |
| -test.mock.method(fs, 'readFile', (file, options, callback) => { |
6 |
| - callback(null, 'Aqur\nBobi\ncat'); |
| 5 | +// Mock the file containing names |
| 6 | +testUtil.mock.method(fs, 'readFile', (file, options, callback) => { |
| 7 | + callback(null, 'Alice\nBob\ncat'); |
7 | 8 | });
|
8 | 9 |
|
9 | 10 | const { Application, MailSystem } = require('./main');
|
10 | 11 |
|
11 |
| -test('MailSystem_write()', () => { |
| 12 | +testUtil('MailSystem_write()', () => { |
12 | 13 | const mailSystem = new MailSystem();
|
13 |
| - assert.strictEqual(mailSystem.write('Aqur'), 'Congrats, Aqur!'); |
| 14 | + assert.strictEqual(mailSystem.write('Alice'), 'Congrats, Alice!'); |
14 | 15 | assert.strictEqual(mailSystem.write(202), 'Congrats, 202!');
|
15 | 16 | assert.strictEqual(mailSystem.write(null), 'Congrats, null!');
|
16 | 17 | });
|
17 | 18 |
|
18 |
| -test('MailSystem_send()', () => { |
| 19 | +testUtil('MailSystem_send()', () => { |
19 | 20 | const mailSystem = new MailSystem();
|
20 |
| - const name = 'Aqur'; |
21 |
| - test.mock.method(Math, 'random', () => 0.9); |
| 21 | + const name = 'Alice'; |
| 22 | + testUtil.mock.method(Math, 'random', () => 0.9); |
22 | 23 | assert.strictEqual(mailSystem.send(name, 'success'), true);
|
23 |
| - test.mock.method(Math, 'random', () => 0.2); |
| 24 | + testUtil.mock.method(Math, 'random', () => 0.2); |
24 | 25 | assert.strictEqual(mailSystem.send(name, 'fail'), false);
|
25 | 26 | });
|
26 | 27 |
|
27 |
| -test('Application_getNames()', async () => { |
| 28 | +testUtil('Application_getNames()', async () => { |
28 | 29 | const app = new Application();
|
29 |
| - const nameList = ['Aqur', 'Bobi', 'cat']; |
| 30 | + const nameList = ['Alice', 'Bob', 'cat']; |
30 | 31 | const [names, selected] = await app.getNames();
|
31 | 32 | assert.deepStrictEqual(names, nameList);
|
32 | 33 | assert.deepStrictEqual(selected, []);
|
33 | 34 | });
|
34 | 35 |
|
35 |
| -test('Application_getRandomPerson()', async () => { |
| 36 | +testUtil('Application_getRandomPerson()', async () => { |
36 | 37 | const app = new Application();
|
37 | 38 | const [names] = await app.getNames();
|
38 | 39 | const randomPerson = app.getRandomPerson();
|
39 | 40 | });
|
40 | 41 |
|
41 |
| -test('Application_selectNextPerson()', async () => { |
| 42 | +testUtil('Application_selectNextPerson()', async () => { |
42 | 43 | const app = new Application();
|
43 | 44 | const [names] = await app.getNames();
|
44 |
| - app.selected = ['Aqur']; |
| 45 | + app.selected = ['Alice']; |
45 | 46 | let count = 0;
|
46 |
| - test.mock.method(app, 'getRandomPerson', () => names[count++]); |
47 |
| - assert.strictEqual(app.selectNextPerson(), 'Bobi'); |
48 |
| - assert.deepStrictEqual(app.selected, ['Aqur', 'Bobi']); |
| 47 | + testUtil.mock.method(app, 'getRandomPerson', () => names[count++]); |
| 48 | + assert.strictEqual(app.selectNextPerson(), 'Bob'); |
| 49 | + assert.deepStrictEqual(app.selected, ['Alice', 'Bob']); |
49 | 50 | assert.strictEqual(app.selectNextPerson(), 'cat');
|
50 |
| - assert.deepStrictEqual(app.selected, ['Aqur', 'Bobi', 'cat']); |
| 51 | + assert.deepStrictEqual(app.selected, ['Alice', 'Bob', 'cat']); |
51 | 52 | assert.strictEqual(app.selectNextPerson(), null);
|
52 | 53 | });
|
53 | 54 |
|
54 |
| - |
55 |
| -test('Application_notifySelected()', async () => { |
| 55 | +testUtil('Application_notifySelected()', async () => { |
56 | 56 | const app = new Application();
|
57 | 57 | const [names] = await app.getNames();
|
58 | 58 | app.selected = names.slice(); // Select all names initially
|
59 |
| - app.mailSystem.send = test.mock.fn(app.mailSystem.send); |
60 |
| - app.mailSystem.write = test.mock.fn(app.mailSystem.write); |
| 59 | + app.mailSystem.send = testUtil.mock.fn(app.mailSystem.send); |
| 60 | + app.mailSystem.write = testUtil.mock.fn(app.mailSystem.write); |
61 | 61 | app.notifySelected();
|
62 | 62 | assert.strictEqual(app.mailSystem.send.mock.calls.length, names.length);
|
63 | 63 | assert.strictEqual(app.mailSystem.write.mock.calls.length, names.length);
|
|
0 commit comments