1
- const test = require ( 'node:test' ) ;
1
+ const { test, mock } = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
+ const fs = require ( 'fs' )
4
+ mock . method ( fs , 'readFile' , ( file , options , callback ) => {
5
+ callback ( null , 'sam\njack\njim' ) ;
6
+ } ) ;
3
7
const { Application, MailSystem } = require ( './main' ) ;
4
8
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
9
+
10
+
11
+ test ( 'write' , ( ) => {
12
+ const mailSystem = new MailSystem ( )
13
+ assert . strictEqual ( mailSystem . write ( 'sam' ) , 'Congrats, sam!' )
14
+ assert . strictEqual ( mailSystem . write ( 123 ) , 'Congrats, 123!' )
15
+ assert . strictEqual ( mailSystem . write ( null ) , 'Congrats, null!' )
16
+
17
+ } ) ;
18
+
19
+
20
+ test ( 'send' , ( ) => {
21
+ const mailSystem = new MailSystem ( )
22
+ mock . method ( Math , 'random' , ( ) => 0.9 ) ;
23
+ assert . strictEqual ( mailSystem . send ( 'sam' , 'hello world' ) , true )
24
+ mock . method ( Math , 'random' , ( ) => 0.1 ) ;
25
+ assert . strictEqual ( mailSystem . send ( 'sam' , 'hello world' ) , false )
26
+ } )
27
+
28
+
29
+ test ( 'getNames' , async ( ) => {
30
+ const app = new Application ( ) ;
31
+ const names = await app . getNames ( ) ;
32
+ assert . deepStrictEqual ( names , [ [ 'sam' , 'jack' , 'jim' ] , [ ] ] ) ;
33
+ } ) ;
34
+
35
+
36
+ test ( 'getRandomPerson' , async ( ) => {
37
+ const app = new Application ( ) ;
38
+ await app . getNames ( ) ;
39
+ mock . method ( Math , 'random' , ( ) => 0.5 )
40
+ const person = app . getRandomPerson ( )
41
+ assert . strictEqual ( person , app . people [ Math . floor ( 0.5 * app . people . length ) ] )
42
+ } )
43
+
44
+
45
+
46
+ test ( 'selectNextPerson' , async ( ) => {
47
+ const app = new Application ( ) ;
48
+ await app . getNames ( ) ;
49
+ app . selected = [ 'sam' ]
50
+ let i = 0
51
+ mock . method ( app , 'getRandomPerson' , ( ) => app . people [ i ++ ] ) ;
52
+ assert . strictEqual ( app . selectNextPerson ( ) , 'jack' )
53
+ assert . strictEqual ( app . selectNextPerson ( ) , 'jim' )
54
+ assert . strictEqual ( app . selectNextPerson ( ) , null )
55
+ } )
56
+
57
+
58
+
59
+ test ( 'notifySelected' , async ( ) => {
60
+ const app = new Application ( ) ;
61
+ const [ names ] = await app . getNames ( ) ;
62
+ app . selected = names . slice ( ) ;
63
+ app . mailSystem . send = mock . fn ( app . mailSystem . send ) ;
64
+ app . mailSystem . write = mock . fn ( app . mailSystem . write ) ;
65
+ app . notifySelected ( ) ;
66
+ assert . strictEqual ( app . mailSystem . send . mock . calls . length , names . length ) ;
67
+ assert . strictEqual ( app . mailSystem . write . mock . calls . length , names . length ) ;
68
+ } )
0 commit comments