Skip to content

[LAB2] 512558012 #604

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

Closed
wants to merge 1 commit into from
Closed
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
81 changes: 77 additions & 4 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,79 @@
const test = require('node:test');
const assert = require('assert');
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const { Application, MailSystem } = require('./main');
const Jasmine = require('jasmine');
const jasmine = new Jasmine();

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
describe('MailSystem', () => {
let mailSystem;

beforeEach(() => {
mailSystem = new MailSystem();
});

it('should write mail with correct context', () => {
const name = 'John Doe';
const context = mailSystem.write(name);
expect(context).toBe('Congrats, ' + name + '!');
});

it('should send mail successfully', () => {
spyOn(console, 'log');
spyOn(Math, 'random').and.returnValue(0.6); // Mocking Math.random to always return a value > 0.5
const success = mailSystem.send('John Doe', 'Congrats, John Doe!');
expect(success).toBe(true);
expect(console.log).toHaveBeenCalledWith('mail sent');
});

it('should fail to send mail', () => {
spyOn(console, 'log');
spyOn(Math, 'random').and.returnValue(0.4); // Mocking Math.random to always return a value <= 0.5
const success = mailSystem.send('John Doe', 'Congrats, John Doe!');
expect(success).toBe(false);
expect(console.log).toHaveBeenCalledWith('mail failed');
});
});

describe('Application', () => {
let app;
let mockReadFile;

beforeEach(() => {
mockReadFile = spyOn(util, 'promisify').and.returnValue(jasmine.createSpy().and.returnValue(Promise.resolve('John Doe\nJane Doe\n')));
app = new Application();
});

it('should get names from file', async () => {
const [people, selected] = await app.getNames();
expect(people).toEqual(['John Doe', 'Jane Doe']);
expect(selected).toEqual([]);
});

it('should select a random person', async () => {
await app.getNames();
spyOn(Math, 'random').and.returnValue(0.5); // Mocking Math.random
const person = app.getRandomPerson();
expect(person).toBe('Jane Doe');
});

it('should select next person', async () => {
await app.getNames();
const person = app.selectNextPerson();
expect(person).toBe('John Doe');
expect(app.selected).toEqual(['John Doe']);
});

it('should notify selected people', async () => {
await app.getNames();
app.selectNextPerson();
spyOn(app.mailSystem, 'write').and.callThrough();
spyOn(app.mailSystem, 'send').and.callThrough();
spyOn(console, 'log');
app.notifySelected();
expect(app.mailSystem.write).toHaveBeenCalledWith('John Doe');
expect(app.mailSystem.send).toHaveBeenCalledWith('John Doe', 'Congrats, John Doe!');
});
});

jasmine.execute();
Loading