Skip to content

Commit e34cc48

Browse files
authored
Merge pull request #148 from SapphireLinXu/312553027
[LAB2] 312553027
2 parents f91e291 + 50532f1 commit e34cc48

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

lab2/main_test.js

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,95 @@ const test = require('node:test');
22
const assert = require('assert');
33
const { Application, MailSystem } = require('./main');
44

5+
6+
const fs = require('fs');
7+
const TestData = 'name_list.txt';
8+
59
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary
10+
// Remember to use Stub, Mock, and Spy when necessary
11+
12+
test('MailSystem should write mail correctly', (t) => {
13+
const writeMock = t.mock.fn(MailSystem.prototype.write);
14+
const context = writeMock('Alice');
15+
assert.strictEqual(context, 'Congrats, Alice!');
16+
});
17+
18+
// test('MailSystem should write mail correctly', () => {
19+
// const mailSystem = new MailSystem();
20+
// const context = mailSystem.write('Alice');
21+
// assert.strictEqual(context, 'Congrats, Alice!');
22+
// });
23+
24+
test('MailSystem should send mail correctly', (t) => {
25+
t.mock.method(Math, 'random').mock.mockImplementation(() => 0.6);
26+
const success = MailSystem.prototype.send('Alice', 'Congrats, Alice!');
27+
assert.strictEqual(success, true);
28+
t.mock.method(Math, 'random').mock.mockImplementation(() => 0.4);
29+
const failure = MailSystem.prototype.send('Alice', 'Congrats, Alice!');
30+
assert.strictEqual(failure, false);
31+
});
32+
33+
34+
test('Application should read file correctly', async (t) => {
35+
36+
fs.writeFileSync(TestData, 'Alice\nBob\nCharlie', 'utf8');
37+
38+
const getNameMock = t.mock.fn(Application.prototype.getNames);
39+
const data = await getNameMock();
40+
41+
assert.strictEqual(data[0].length, 3);
42+
assert.strictEqual(data[0][0], 'Alice');
43+
44+
if (fs.existsSync(TestData)) {
45+
fs.unlinkSync(TestData);
46+
}
47+
});
48+
49+
test('Application should get random person', async (t) => {
50+
const getNamesMock = t.mock.method(Application.prototype, 'getNames', async () => {
51+
return [['Alice', 'Bob', 'Charlie'], []];
52+
});
53+
const app = new Application();
54+
const person = await app.getNames();
55+
56+
assert.strictEqual(getNamesMock.mock.callCount(), 2);
57+
58+
const randomPerson = app.getRandomPerson();
59+
assert.strictEqual(app.people.includes(randomPerson), true);
60+
61+
});
62+
63+
test('Application should select next person', async (t) => {
64+
const getNamesMock = t.mock.method(Application.prototype, 'getNames', async () => {
65+
return [['Alice', 'Bob', 'Charlie'], []];
66+
});
67+
const app = new Application();
68+
const person = await app.getNames();
69+
70+
const selectNextPerson = app.selectNextPerson();
71+
assert.strictEqual(app.selected.includes(selectNextPerson), true);
72+
app.selectNextPerson();
73+
app.selectNextPerson();
74+
console.log(app.selected);
75+
assert.strictEqual(app.selectNextPerson(), null);
76+
});
77+
78+
test('Application should notify person', async (t) => {
79+
const nameList = ['Alice', 'Bob', 'Charlie'];
80+
t.mock.method(Application.prototype, 'getNames', async () => {
81+
return [nameList, []];
82+
});
83+
const app = new Application();
84+
await app.getNames();
85+
86+
app.selected = nameList;
87+
const writeMock = t.mock.method(MailSystem.prototype, 'write', () => {
88+
return true;
89+
});
90+
const sendMock = t.mock.method(MailSystem.prototype, 'send', () => {
91+
return true;
92+
});
93+
app.notifySelected();
94+
assert.strictEqual(writeMock.mock.callCount(), app.selected.length);
95+
assert.strictEqual(sendMock.mock.callCount(), app.selected.length);
96+
});

0 commit comments

Comments
 (0)