-
-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathOOP.test.ts
More file actions
50 lines (39 loc) · 1.81 KB
/
Copy pathOOP.test.ts
File metadata and controls
50 lines (39 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { MailService, GmailService } from '../Abstraction';
import { SmartWatch } from '../Encapsulation';
import { User, Admin } from '../Inheritance';
import { UIElement, TextBox, Checkbox } from '../Polymorphism';
describe('TypeScript OOP Educational Examples', () => {
test('Abstraction: Should correctly use MailService and GmailService', () => {
const gmail = new GmailService();
const spy = jest.spyOn(console, 'log');
gmail.send();
expect(spy).toHaveBeenCalledWith("Connecting to mail server...");
expect(spy).toHaveBeenCalledWith("Sending mail via Gmail...");
spy.mockRestore();
});
test('Encapsulation: Should protect internal step count', () => {
const watch = new SmartWatch();
watch.addSteps(100);
expect(watch.steps).toBe(100);
watch.addSteps(-50); // should not reduce steps because step must be > 0
expect(watch.steps).toBe(100);
// watch._stepCount is inaccessible here (TypeScript error)
});
test('Inheritance: Should allow Admin to inherit User and have custom behavior', () => {
const admin = new Admin("Alice");
const spy = jest.spyOn(console, 'log');
admin.login();
admin.deleteUser("Bob");
expect(spy).toHaveBeenCalledWith("Alice logged in.");
expect(spy).toHaveBeenCalledWith("Admin Alice is deleting user: Bob");
spy.mockRestore();
});
test('Polymorphism: Should work with different UIElement types', () => {
const elements: UIElement[] = [new TextBox(), new Checkbox()];
const spy = jest.spyOn(console, 'log');
elements.forEach(el => el.render());
expect(spy).toHaveBeenCalledWith("Rendering a TextBox");
expect(spy).toHaveBeenCalledWith("Rendering a Checkbox");
spy.mockRestore();
});
});