Skip to content

[LAB2] 313551801 #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 313551801
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions lab2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ class MailSystem {

class Application {
constructor() {
this.people = [];
this.selected = [];
this.mailSystem = new MailSystem();
this.getNames().then(([people, selected]) => {
this.people = people;
this.selected = selected;
});
this.people = [];
this.selected = [];
this.mailSystem = new MailSystem();
}

async getNames() {
const data = await readFile('name_list.txt', 'utf8');
const people = data.split('\n');
const selected = [];
return [people, selected];
const data = await readFile('name_list.txt', 'utf8');
this.people = data.split('\n');
this.selected = [];
}

getRandomPerson() {
Expand Down
56 changes: 54 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');
const sinon = require('sinon');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
test('test getRandomPerson in main.js', async () => {
const app = new Application();

app.getNames = async () => {
app.people = ['Annika', 'Billy', 'Cecilia'];
};

await app.getNames();

const person = app.getRandomPerson();
assert.ok(app.people.includes(person), 'the person is not in list');
});

test('test mains selectNextPerson to not select same person twice', async () => {
const app = new Application();

app.getNames = async () => {
app.people = ['Annika', 'Billy', 'Cecilia'];
app.selected = [];
};

await app.getNames();

const person1 = app.selectNextPerson();
const person2 = app.selectNextPerson();

assert.notStrictEqual(person1, person2, 'Same person was selected twice');
});

test('test mains notifySelected that it calls send()', async () => {
const mailSystem = new MailSystem();
const sendSpy = sinon.spy(mailSystem, 'send');

const app = new Application();
app.mailSystem = mailSystem;

app.getNames = async () => {
app.people = ['Annika', 'Billy', 'Cecilia'];
app.selected = [];
};

await app.getNames();

const person = app.selectNextPerson();
app.selected = [person];

await app.notifySelected();

assert.ok(
sendSpy.calledWith(person, `Congrats, ${person}!`),
`send() didn't work with args. Captured calls: ${JSON.stringify(sendSpy.args)}`
);
});
Loading