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