Skip to content

Commit 3d40321

Browse files
committed
lab2 completed
1 parent e904fe6 commit 3d40321

4 files changed

Lines changed: 223 additions & 9 deletions

File tree

lab2/main.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,15 @@ class Application {
2828
this.people = [];
2929
this.selected = [];
3030
this.mailSystem = new MailSystem();
31-
this.getNames().then(([people, selected]) => {
32-
this.people = people;
33-
this.selected = selected;
34-
});
3531
}
3632

3733
async getNames() {
3834
const data = await readFile('name_list.txt', 'utf8');
39-
const people = data.split('\n');
40-
const selected = [];
41-
return [people, selected];
35+
this.people = data.split('\n');
36+
this.selected = [];
4237
}
4338

39+
4440
getRandomPerson() {
4541
const i = Math.floor(Math.random() * this.people.length);
4642
return this.people[i];

lab2/main_test.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,57 @@
11
const test = require('node:test');
22
const assert = require('assert');
33
const { Application, MailSystem } = require('./main');
4+
const sinon = require('sinon');
45

5-
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary
6+
test('test getRandomPerson in main.js', async () => {
7+
const app = new Application();
8+
9+
app.getNames = async () => {
10+
app.people = ["Annika", "Billy", "Cecilia"];
11+
app.selected = [];
12+
};
13+
14+
await app.getNames();
15+
16+
const person = app.getRandomPerson();
17+
assert.ok(app.people.includes(person), 'the person is not in list');
18+
});
19+
20+
21+
test('test mains selectNextPerson to not select same person twice', async () => {
22+
const app = new Application();
23+
24+
app.getNames = async () => {
25+
app.people = ["Annika", "Billy", "Cecilia"];
26+
app.selected = [];
27+
};
28+
29+
await app.getNames();
30+
31+
const person1 = app.selectNextPerson();
32+
const person2 = app.selectNextPerson();
33+
34+
assert.notStrictEqual(person1, person2, 'Same person was selected twice');
35+
});
36+
37+
test('test mains notifySelected that it calls send()', async () => {
38+
const mailSystem = new MailSystem();
39+
const sendSpy = sinon.spy(mailSystem, 'send');
40+
41+
const app = new Application();
42+
app.mailSystem = mailSystem;
43+
44+
app.getNames = async () => {
45+
app.people = ["Annika", "Billy", "Cecilia"];
46+
app.selected = [];
47+
};
48+
49+
await app.getNames();
50+
51+
const person = app.selectNextPerson();
52+
app.selected = [person];
53+
54+
await app.notifySelected();
55+
56+
assert.ok(sendSpy.calledWith(person, `Congrats, ${person}!`), `send() didn't work with args. Captured calls: ${JSON.stringify(sendSpy.args)}`);
57+
});

lab2/package-lock.json

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lab2/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"sinon": "^19.0.4"
4+
}
5+
}

0 commit comments

Comments
 (0)