diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..2bb19a34 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -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"); -}); \ No newline at end of file + // 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"); +}); diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468e..1d09ee75 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -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 \ No newline at end of file +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); +});