Skip to content

Commit d4c0246

Browse files
committed
finish 99% of lab2
1 parent 57dd5c7 commit d4c0246

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

lab2/main_test.js

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

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

0 commit comments

Comments
 (0)