|
1 | 1 | const test = require('node:test');
|
2 | 2 | const assert = require('assert');
|
3 | 3 | const fs = require('fs');
|
4 |
| -const { Application, MailSystem } = require('./main'); |
5 |
| -const sinon = require('sinon'); |
6 |
| - |
7 |
| -// Mock fs.readFile |
8 |
| -sinon.stub(fs, 'readFile').callsFake((file, options, callback) => { |
9 |
| - callback(null, 'Guan\nChen\nGala\n'); |
| 4 | +test.mock.method(fs, 'readFile', (file, options, callback) => { |
| 5 | + callback(null, 'Guan\nChen\nGala'); |
10 | 6 | });
|
| 7 | +const { Application, MailSystem } = require('./main'); |
11 | 8 |
|
12 |
| -test("MailSystem write", () => { |
13 |
| - const mailSystem = new MailSystem(); |
14 |
| - assert.strictEqual(mailSystem.write("Guan"), "Congrats, Guan!"); |
| 9 | +// TODO: write your tests here |
| 10 | +// Remember to use Stub, Mock, and Spy when necessary |
| 11 | +test("Test MailSystem's write", () => { |
| 12 | + const myClass = new MailSystem() |
| 13 | + assert.strictEqual( myClass.write("Guan"),"Congrats, Guan!") |
15 | 14 | });
|
16 | 15 |
|
17 |
| -test("MailSystem send", () => { |
18 |
| - const mailSystem = new MailSystem(); |
19 |
| - const originalMathRandom = Math.random; |
20 |
| - |
21 |
| - Math.random = () => 0.6; |
22 |
| - const context = mailSystem.write("Guan"); |
23 |
| - assert.strictEqual(mailSystem.send("Guan", context), true); |
24 |
| - |
25 |
| - Math.random = () => 0.4; |
26 |
| - assert.strictEqual(mailSystem.send("Chen", context), false); |
27 |
| - |
28 |
| - Math.random = originalMathRandom; |
| 16 | +test("Test MailSystem's send(name, context)", () => { |
| 17 | + Math.random = () => 0.6 |
| 18 | + const mailSystem = new MailSystem() |
| 19 | + const context = mailSystem.write("Guan") |
| 20 | + const success = mailSystem.send("Guan",context) |
| 21 | + assert.strictEqual(success,true) |
| 22 | + Math.random = () => 0.5 |
| 23 | + assert.strictEqual(mailSystem.send("Chen",context),false) |
29 | 24 | });
|
30 | 25 |
|
31 |
| -test("Application getNames", async () => { |
32 |
| - const application = new Application(); |
33 |
| - const [people, selected] = await application.getNames(); |
34 |
| - // Remove any empty strings from the array |
35 |
| - assert.deepStrictEqual(people.filter(name => name), ["Guan", "Chen", "Gala"]); |
36 |
| - assert.deepStrictEqual(selected, []); |
| 26 | +test("Test Application's getNames", async () => { |
| 27 | + const application = new Application |
| 28 | + const [people, selected] = await application.getNames() |
| 29 | + assert.deepStrictEqual(people, ["Guan", "Chen", "Gala"]) |
| 30 | + assert.deepStrictEqual(selected, []) |
37 | 31 | });
|
38 | 32 |
|
39 |
| -test("Application getRandomPerson", () => { |
40 |
| - const application = new Application(); |
41 |
| - application.people = ["Guan", "Chen", "Gala"]; |
42 |
| - Math.random = () => 0; |
43 |
| - assert.deepStrictEqual(application.getRandomPerson(), "Guan"); |
44 |
| - Math.random = () => 0.6; |
45 |
| - assert.deepStrictEqual(application.getRandomPerson(), "Chen"); |
46 |
| - Math.random = () => 0.9; |
47 |
| - assert.deepStrictEqual(application.getRandomPerson(), "Gala"); |
| 33 | +test("Test Application's getRandomPerson()", (t) => { |
| 34 | + const application = new Application |
| 35 | + application.people = ["Guan", "Chen", "Gala"] |
| 36 | + Math.random = () => 0 |
| 37 | + assert.deepStrictEqual(application.getRandomPerson(), "Guan"); |
| 38 | + Math.random = () => 0.6 |
| 39 | + assert.deepStrictEqual(application.getRandomPerson(), "Chen"); |
| 40 | + Math.random = () => 0.9 |
| 41 | + assert.deepStrictEqual(application.getRandomPerson(), "Gala"); |
48 | 42 | });
|
49 | 43 |
|
50 |
| -test("Application selectNextPerson", async () => { |
51 |
| - const application = new Application(); |
52 |
| - await application.getNames(); |
53 |
| - application.selected = ["Guan"]; |
54 |
| - let count = 1; |
55 |
| - const getRandomPersonStub = sinon.stub(application, 'getRandomPerson').callsFake(() => { |
56 |
| - const name_list = application.people; |
57 |
| - if (count < name_list.length) { |
58 |
| - return name_list[count++]; |
59 |
| - } else { |
60 |
| - return null; |
61 |
| - } |
62 |
| - }); |
63 |
| - |
64 |
| - assert.strictEqual(application.selectNextPerson(), "Chen"); |
65 |
| - assert.strictEqual(application.selectNextPerson(), "Gala"); |
66 |
| - assert.strictEqual(application.selectNextPerson(), null); |
67 |
| - |
68 |
| - getRandomPersonStub.restore(); |
| 44 | +test("Test Application's selectNextPerson", async() => { |
| 45 | + const application = new Application |
| 46 | + const name_list = await application.getNames(); |
| 47 | + application.selected = ["Guan"] |
| 48 | + let count = 0; |
| 49 | + test.mock.method(application, 'getRandomPerson', () => { |
| 50 | + if (count <= name_list.length) { |
| 51 | + return name_list[0][count++]; |
| 52 | + } |
| 53 | + }); |
| 54 | + assert.strictEqual(application.selectNextPerson(), "Chen") |
| 55 | + assert.strictEqual(application.selectNextPerson(), "Gala") |
| 56 | + assert.strictEqual(application.selectNextPerson(), null) |
69 | 57 | });
|
70 | 58 |
|
71 |
| -test("Application notifySelected", () => { |
72 |
| - const application = new Application(); |
73 |
| - application.people = ["Guan", "Chen", "Gala"]; |
74 |
| - application.selected = ["Guan", "Chen", "Gala"]; |
75 |
| - sinon.spy(application.mailSystem, 'write'); |
76 |
| - sinon.spy(application.mailSystem, 'send'); |
77 |
| - application.notifySelected(); |
78 |
| - assert.strictEqual(application.mailSystem.write.callCount, application.people.length); |
79 |
| - assert.strictEqual(application.mailSystem.send.callCount, application.people.length); |
| 59 | +test("Test Application's notifySelected() ", () => { |
| 60 | + const application = new Application |
| 61 | + application.people = ["Guan", "Chen", "Gala"] |
| 62 | + application.selected = ["Guan", "Chen", "Gala"] |
| 63 | + application.mailSystem.write = test.mock.fn(application.mailSystem.write) |
| 64 | + application.mailSystem.send = test.mock.fn(application.mailSystem.send) |
| 65 | + application.notifySelected() |
| 66 | + assert.strictEqual(application.mailSystem.send.mock.calls.length, application.people.length); |
| 67 | + assert.strictEqual(application.mailSystem.write.mock.calls.length, application.people.length); |
80 | 68 | });
|
0 commit comments