Skip to content

Commit 1ea77ed

Browse files
authored
Merge pull request #68 from forbis/511558013
[LAB2] 511558013
2 parents 2e671aa + 0efda56 commit 1ea77ed

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

lab2/main_test.js

+61-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,64 @@ test.mock.method(fs, 'readFile', (file, options, callback) => {
66
});
77
const { Application, MailSystem } = require('./main');
88

9-
// TODO: write your tests here
10-
// Remember to use Stub, Mock, and Spy when necessary
9+
test('Test MailSystem : write()', () => {
10+
const ms = new MailSystem();
11+
assert.strictEqual(ms.write('alpha'), 'Congrats, alpha!');
12+
assert.strictEqual(ms.write(null), 'Congrats, null!');
13+
assert.strictEqual(ms.write(48763), 'Congrats, 48763!');
14+
});
15+
16+
test('Test MailSystem : send()', () => {
17+
const ms = new MailSystem();
18+
const name = 'alpha';
19+
test.mock.method(Math, 'random', () => 0.6);
20+
assert.strictEqual(ms.send(name, 'success'), true);
21+
test.mock.method(Math, 'random', () => 0.4);
22+
assert.strictEqual(ms.send(name, 'fail'), false);
23+
});
24+
25+
test('Test Application : getNames()', async () => {
26+
const app = new Application();
27+
const name_list = ['alpha', 'beta', 'gama'];
28+
const names = await app.getNames();
29+
assert.deepStrictEqual(names, [name_list, []])
30+
});
31+
32+
test('Test Application : getRandomPerson()', async (test) => {
33+
const app = new Application();
34+
const names = await app.getNames();
35+
test.mock.method(Math, 'random', () => 0);
36+
assert.strictEqual(app.getRandomPerson(), 'alpha');
37+
test.mock.method(Math, 'random', () => 0.4);
38+
assert.strictEqual(app.getRandomPerson(), 'beta');
39+
test.mock.method(Math, 'random', () => 0.7);
40+
assert.strictEqual(app.getRandomPerson(), 'gama');
41+
});
42+
43+
test('Test Application : selectNextPerson()', async (test) => {
44+
const app = new Application();
45+
const names = await app.getNames();
46+
app.selected = ['alpha'];
47+
let cnt = 0;
48+
test.mock.method(app, 'getRandomPerson', () => {
49+
if (cnt <= names.length) {
50+
return names[0][cnt++];
51+
}
52+
});
53+
assert.strictEqual(app.selectNextPerson(), 'beta');
54+
assert.deepStrictEqual(app.selected, ['alpha', 'beta']);
55+
assert.strictEqual(app.selectNextPerson(), 'gama');
56+
assert.deepStrictEqual(app.selected, ['alpha', 'beta', 'gama']);
57+
assert.strictEqual(app.selectNextPerson(), null);
58+
});
59+
60+
test('Test Application : notifySelected()', async (test) => {
61+
const app = new Application();
62+
app.people = ['alpha', 'beta', 'gama'];
63+
app.selected = ['alpha', 'beta', 'gama'];
64+
app.mailSystem.send = test.mock.fn(app.mailSystem.send);
65+
app.mailSystem.write = test.mock.fn(app.mailSystem.write);
66+
app.notifySelected();
67+
assert.strictEqual(app.mailSystem.send.mock.calls.length, 3);
68+
assert.strictEqual(app.mailSystem.write.mock.calls.length, 3);
69+
});

0 commit comments

Comments
 (0)