Skip to content

[LAB1] 511558024 #64

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

Merged
merged 13 commits into from
Mar 20, 2024
55 changes: 46 additions & 9 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,58 @@ const assert = require('assert');
const { MyClass, Student } = require('./main');

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
// Test adding a valid student
const myClass = new MyClass();
const student1 = new Student();
assert.strictEqual(myClass.addStudent(student1), 0);
assert.strictEqual(myClass.students.length, 1);
assert.strictEqual(myClass.students[0], student1);

// Test adding another valid student
const student2 = new Student();
assert.strictEqual(myClass.addStudent(student2), 1);
assert.strictEqual(myClass.students.length, 2);
assert.strictEqual(myClass.students[1], student2);

// Test adding an invalid student
assert.strictEqual(myClass.addStudent({}), -1);
assert.strictEqual(myClass.students.length, 2); // Should not add invalid student
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
// Test getting student by valid id
const myClass = new MyClass();
const student1 = new Student();
const student2 = new Student();

myClass.addStudent(student1);
myClass.addStudent(student2);

assert.strictEqual(myClass.getStudentById(0), student1);
assert.strictEqual(myClass.getStudentById(1), student2);

// Test getting student by invalid id
assert.strictEqual(myClass.getStudentById(-1), null);
assert.strictEqual(myClass.getStudentById(2), null);
});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
// Test setting valid name
const student = new Student();
student.setName("CHINA");
assert.strictEqual(student.name, "CHINA");

// Test setting invalid name
student.setName(8081);
assert.strictEqual(student.name, "CHINA"); // Should not change name if invalid
});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
});
// Test getting name when name is not set
const student = new Student();
assert.strictEqual(student.getName(), "");

// Test getting name when name is set
student.setName("CUBA");
assert.strictEqual(student.getName(), "CUBA");
});
45 changes: 43 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,46 @@ const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
test("Test Application's selectNextPerson", () => {
const app = new Application();
const initialSelectedLength = app.selected.length;
const selectedPerson = app.selectNextPerson();
assert.strictEqual(app.selected.length, initialSelectedLength + 1, "One person should be selected");
});

test("Test Application's notifySelected", () => {
const app = new Application();
const consoleSpy = jest.spyOn(console, 'log');
app.notifySelected();
expect(consoleSpy).toHaveBeenCalled();
});

test("Test MailSystem's write method", () => {
const mailSystem = new MailSystem();
const name = "John";
const context = mailSystem.write(name);
assert.strictEqual(context, `Congrats, ${name}!`, "Context should match expected");
});

test("Test MailSystem's send method", () => {
const mailSystem = new MailSystem();
const name = "John";
const context = "Test context";
const result = mailSystem.send(name, context);
assert(result === true || result === false, "send method should return a boolean");
});

test("Test Application's getRandomPerson", () => {
const app = new Application();
const getRandomPersonSpy = jest.spyOn(app, 'getRandomPerson');
app.getRandomPerson();
expect(getRandomPersonSpy).toHaveBeenCalled();
});

test("Test Application's getRandomPerson with stubbed getNames method", () => {
const app = new Application();
const stubbedNames = ['John', 'Jane', 'Doe'];
app.getNames = jest.fn().mockResolvedValue([stubbedNames, []]);
const randomPerson = app.getRandomPerson();
expect(stubbedNames).toContain(randomPerson);
});
Loading