Skip to content

Commit 8df3f90

Browse files
committed
Resolving git branch problems for lab
1 parent b630cfa commit 8df3f90

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

lab2/main.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,15 @@ class MailSystem {
2525

2626
class Application {
2727
constructor() {
28-
this.people = [];
29-
this.selected = [];
30-
this.mailSystem = new MailSystem();
31-
this.getNames().then(([people, selected]) => {
32-
this.people = people;
33-
this.selected = selected;
34-
});
28+
this.people = [];
29+
this.selected = [];
30+
this.mailSystem = new MailSystem();
3531
}
36-
32+
3733
async getNames() {
38-
const data = await readFile('name_list.txt', 'utf8');
39-
const people = data.split('\n');
40-
const selected = [];
41-
return [people, selected];
34+
const data = await readFile('name_list.txt', 'utf8');
35+
this.people = data.split('\n');
36+
this.selected = [];
4237
}
4338

4439
getRandomPerson() {

lab2/main_test.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
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+
};
12+
13+
await app.getNames();
14+
15+
const person = app.getRandomPerson();
16+
assert.ok(app.people.includes(person), 'the person is not in list');
17+
});
18+
19+
test('test mains selectNextPerson to not select same person twice', async () => {
20+
const app = new Application();
21+
22+
app.getNames = async () => {
23+
app.people = ['Annika', 'Billy', 'Cecilia'];
24+
app.selected = [];
25+
};
26+
27+
await app.getNames();
28+
29+
const person1 = app.selectNextPerson();
30+
const person2 = app.selectNextPerson();
31+
32+
assert.notStrictEqual(person1, person2, 'Same person was selected twice');
33+
});
34+
35+
test('test mains notifySelected that it calls send()', async () => {
36+
const mailSystem = new MailSystem();
37+
const sendSpy = sinon.spy(mailSystem, 'send');
38+
39+
const app = new Application();
40+
app.mailSystem = mailSystem;
41+
42+
app.getNames = async () => {
43+
app.people = ['Annika', 'Billy', 'Cecilia'];
44+
app.selected = [];
45+
};
46+
47+
await app.getNames();
48+
49+
const person = app.selectNextPerson();
50+
app.selected = [person];
51+
52+
await app.notifySelected();
53+
54+
assert.ok(
55+
sendSpy.calledWith(person, `Congrats, ${person}!`),
56+
`send() didn't work with args. Captured calls: ${JSON.stringify(sendSpy.args)}`
57+
);
58+
});

0 commit comments

Comments
 (0)