Skip to content

Commit

Permalink
Update main_test.js
Browse files Browse the repository at this point in the history
lab2
  • Loading branch information
SUNGOD3 authored Mar 11, 2024
1 parent 5d7d397 commit bb4ff2a
Showing 1 changed file with 96 additions and 2 deletions.
98 changes: 96 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,100 @@
const test = require('node:test');
const assert = require('assert');
const fs = require('fs');
const { promisify } = require('util');
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
const writeFile = promisify(fs.writeFile);

//test.mock.method(fs, 'readFile', async (file, encoding) => {

test("Test MailSystem's write", () => {
const mailSystem = new MailSystem();
const context = mailSystem.write('a');
assert.strictEqual(context, 'Congrats, a!');
});

test("Test MailSystem's send", () => {
const mailSystem = new MailSystem();
const originalMathRandom = Math.random;
Math.random = () => 0.51;
const result = mailSystem.send('SUNGOD', 'Test context');
assert.strictEqual(result, true);
Math.random = () => 0.49;
const result2 = mailSystem.send('SUNGOD', 'Test context');
assert.strictEqual(result2, false);
Math.random = originalMathRandom;
});

test("Test Application's getRandom&selectNextPerson", async () => {
const fakeData = 'John\nJane';
const fakeFile = 'name_list.txt';
await writeFile(fakeFile, fakeData, 'utf8');

const application = new Application();
const [people, selected] = await application.getNames();

assert.deepStrictEqual(people, ['John', 'Jane']);
assert.deepStrictEqual(selected, []);

const rd_person = application.getRandomPerson();
assert.ok(people.includes(rd_person));

application.getRandomPerson = () => 'John';
let person = application.selectNextPerson();
assert.deepStrictEqual(application.selected, ['John']);

// Select the next person
application.getRandomPerson = () => 'Jane';
person = application.selectNextPerson();
assert.deepStrictEqual(application.selected, ['John', 'Jane']);

// All people are selected
person = application.selectNextPerson();
assert.strictEqual(person, null);

let cnt = 0;
application.getRandomPerson = () => {
if (cnt++ % 2 === 0) {
return 'John';
} else {
return 'Jane';
}
}
application.selected = ['John'];
person = application.selectNextPerson();
assert.strictEqual(person, 'Jane');

fs.unlinkSync(fakeFile);
});

test("Test Application's notifySelected", async () => {
const fakeData = 'John\nJane\nDoe\nSmith';
const fakeFile = 'name_list.txt';
await writeFile(fakeFile, fakeData, 'utf8');

const application = new Application();
const [people, selected] = await application.getNames();

assert.deepStrictEqual(people, ['John', 'Jane', 'Doe', 'Smith']);
assert.deepStrictEqual(selected, []);

// Create a spy for the MailSystem's write and send methods
const mailSystemSpy = {
write: test.mock.fn(MailSystem.prototype.write),
send: test.mock.fn(MailSystem.prototype.send)
};
application.mailSystem = mailSystemSpy;
application.selected = ['John', 'Jane', 'Doe'];

application.notifySelected();

// Ensure that write and send methods are called for each selected person
assert.strictEqual(mailSystemSpy.write.mock.callCount(), 3);
assert.strictEqual(mailSystemSpy.send.mock.callCount(), 3);

assert.deepStrictEqual(mailSystemSpy.write.mock.calls.map(call => call.arguments[0]), ['John', 'Jane', 'Doe']);
assert.deepStrictEqual(mailSystemSpy.send.mock.calls.map(call => call.arguments[0]), ['John', 'Jane', 'Doe']);

fs.unlinkSync(fakeFile);
});

0 comments on commit bb4ff2a

Please sign in to comment.