1
- const test = require ( 'node:test' ) ;
2
- const assert = require ( 'assert' ) ;
1
+ const fs = require ( 'fs' ) ;
2
+ const util = require ( 'util' ) ;
3
+ const readFile = util . promisify ( fs . readFile ) ;
3
4
const { Application, MailSystem } = require ( './main' ) ;
5
+ const Jasmine = require ( 'jasmine' ) ;
6
+ const jasmine = new Jasmine ( ) ;
4
7
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
8
+ describe ( 'MailSystem' , ( ) => {
9
+ let mailSystem ;
10
+
11
+ beforeEach ( ( ) => {
12
+ mailSystem = new MailSystem ( ) ;
13
+ } ) ;
14
+
15
+ it ( 'should write mail with correct context' , ( ) => {
16
+ const name = 'John Doe' ;
17
+ const context = mailSystem . write ( name ) ;
18
+ expect ( context ) . toBe ( 'Congrats, ' + name + '!' ) ;
19
+ } ) ;
20
+
21
+ it ( 'should send mail successfully' , ( ) => {
22
+ spyOn ( console , 'log' ) ;
23
+ spyOn ( Math , 'random' ) . and . returnValue ( 0.6 ) ; // Mocking Math.random to always return a value > 0.5
24
+ const success = mailSystem . send ( 'John Doe' , 'Congrats, John Doe!' ) ;
25
+ expect ( success ) . toBe ( true ) ;
26
+ expect ( console . log ) . toHaveBeenCalledWith ( 'mail sent' ) ;
27
+ } ) ;
28
+
29
+ it ( 'should fail to send mail' , ( ) => {
30
+ spyOn ( console , 'log' ) ;
31
+ spyOn ( Math , 'random' ) . and . returnValue ( 0.4 ) ; // Mocking Math.random to always return a value <= 0.5
32
+ const success = mailSystem . send ( 'John Doe' , 'Congrats, John Doe!' ) ;
33
+ expect ( success ) . toBe ( false ) ;
34
+ expect ( console . log ) . toHaveBeenCalledWith ( 'mail failed' ) ;
35
+ } ) ;
36
+ } ) ;
37
+
38
+ describe ( 'Application' , ( ) => {
39
+ let app ;
40
+ let mockReadFile ;
41
+
42
+ beforeEach ( ( ) => {
43
+ mockReadFile = spyOn ( util , 'promisify' ) . and . returnValue ( jasmine . createSpy ( ) . and . returnValue ( Promise . resolve ( 'John Doe\nJane Doe\n' ) ) ) ;
44
+ app = new Application ( ) ;
45
+ } ) ;
46
+
47
+ it ( 'should get names from file' , async ( ) => {
48
+ const [ people , selected ] = await app . getNames ( ) ;
49
+ expect ( people ) . toEqual ( [ 'John Doe' , 'Jane Doe' ] ) ;
50
+ expect ( selected ) . toEqual ( [ ] ) ;
51
+ } ) ;
52
+
53
+ it ( 'should select a random person' , async ( ) => {
54
+ await app . getNames ( ) ;
55
+ spyOn ( Math , 'random' ) . and . returnValue ( 0.5 ) ; // Mocking Math.random
56
+ const person = app . getRandomPerson ( ) ;
57
+ expect ( person ) . toBe ( 'Jane Doe' ) ;
58
+ } ) ;
59
+
60
+ it ( 'should select next person' , async ( ) => {
61
+ await app . getNames ( ) ;
62
+ const person = app . selectNextPerson ( ) ;
63
+ expect ( person ) . toBe ( 'John Doe' ) ;
64
+ expect ( app . selected ) . toEqual ( [ 'John Doe' ] ) ;
65
+ } ) ;
66
+
67
+ it ( 'should notify selected people' , async ( ) => {
68
+ await app . getNames ( ) ;
69
+ app . selectNextPerson ( ) ;
70
+ spyOn ( app . mailSystem , 'write' ) . and . callThrough ( ) ;
71
+ spyOn ( app . mailSystem , 'send' ) . and . callThrough ( ) ;
72
+ spyOn ( console , 'log' ) ;
73
+ app . notifySelected ( ) ;
74
+ expect ( app . mailSystem . write ) . toHaveBeenCalledWith ( 'John Doe' ) ;
75
+ expect ( app . mailSystem . send ) . toHaveBeenCalledWith ( 'John Doe' , 'Congrats, John Doe!' ) ;
76
+ } ) ;
77
+ } ) ;
78
+
79
+ jasmine . execute ( ) ;
0 commit comments