@@ -7,9 +7,18 @@ const writeFile = util.promisify(fs.writeFile);
7
7
const { Application, MailSystem } = require ( './main' ) ;
8
8
const path = require ( 'node:path' ) ;
9
9
10
- // TODO: write your tests here
11
- // Remember to use Stub, Mock, and Spy when necessary
12
- // Remember to use Stub, Mock, and Spy when necessary
10
+ // Mock fs.unlinkSync
11
+ const originalUnlinkSync = fs . unlinkSync ;
12
+ fs . unlinkSync = ( path ) => {
13
+ console . log ( `Mocked unlinkSync called with path: ${ path } ` ) ;
14
+ } ;
15
+
16
+ // Mock fs.readFileSync
17
+ const originalReadFileSync = fs . readFileSync ;
18
+ fs . readFileSync = ( path , encoding ) => {
19
+ console . log ( `Mocked readFileSync called with path: ${ path } and encoding: ${ encoding } ` ) ;
20
+ return "JJ\nEE\nRR\nYY" ;
21
+ } ;
13
22
14
23
test ( "Test MailSystem's write" , ( ) => {
15
24
const ms = new MailSystem ( ) ;
@@ -19,7 +28,8 @@ test("Test MailSystem's write", () => {
19
28
20
29
test ( "Test MailSystem's send" , ( ) => {
21
30
const ms = new MailSystem ( ) ;
22
- const orgrdm = Math . random ;
31
+ const originalMathRandom = Math . random ;
32
+
23
33
Math . random = ( ) => 0.4 ;
24
34
let context = ms . send ( "Jerry" , "Test Message" ) ;
25
35
assert . strictEqual ( context , false ) ;
@@ -28,7 +38,7 @@ test("Test MailSystem's send", () => {
28
38
context = ms . send ( "Jerry" , "Test Message" ) ;
29
39
assert . strictEqual ( context , true ) ;
30
40
31
- Math . random = orgrdm ;
41
+ Math . random = originalMathRandom ;
32
42
} ) ;
33
43
34
44
test ( "Test Application's getNames" , async ( ) => {
@@ -38,12 +48,11 @@ test("Test Application's getNames", async () => {
38
48
39
49
const app = new Application ( ) ;
40
50
41
- let ctx = new Array ( [ ] , [ ] ) ;
42
- ctx = await app . getNames ( ) ;
51
+ let ctx = await app . getNames ( ) ;
43
52
assert . deepStrictEqual ( ctx [ 0 ] , [ "JJ" , "EE" , "RR" , "YY" ] ) ;
44
53
assert . deepStrictEqual ( ctx [ 1 ] , [ ] ) ;
45
54
46
- fs . unlinkSync ( fn_ ) ;
55
+ originalUnlinkSync ( fn_ ) ;
47
56
} ) ;
48
57
49
58
test ( "Test Application's getRandomPerson" , async ( ) => {
@@ -53,8 +62,7 @@ test("Test Application's getRandomPerson", async () => {
53
62
54
63
const app = new Application ( ) ;
55
64
56
- let ctx = new Array ( [ ] , [ ] ) ;
57
- ctx = await app . getNames ( ) ;
65
+ let ctx = await app . getNames ( ) ;
58
66
assert . deepStrictEqual ( ctx [ 0 ] , [ "JJ" , "EE" ] ) ;
59
67
assert . deepStrictEqual ( ctx [ 1 ] , [ ] ) ;
60
68
@@ -64,7 +72,7 @@ test("Test Application's getRandomPerson", async () => {
64
72
rdmPeople = await app . getRandomPerson ( ) ;
65
73
assert . ok ( app . people . includes ( rdmPeople ) ) ;
66
74
67
- fs . unlinkSync ( fn_ ) ;
75
+ originalUnlinkSync ( fn_ ) ;
68
76
} ) ;
69
77
70
78
test ( "Test Application's selectNextPerson" , async ( ) => {
@@ -73,8 +81,7 @@ test("Test Application's selectNextPerson", async () => {
73
81
await writeFile ( fn_ , data_ , "utf-8" ) ;
74
82
75
83
const app = new Application ( ) ;
76
- let ctx = new Array ( [ ] , [ ] ) ;
77
- ctx = await app . getNames ( ) ;
84
+ let ctx = await app . getNames ( ) ;
78
85
assert . deepStrictEqual ( ctx [ 0 ] , [ "JJ" , "EE" ] ) ;
79
86
assert . deepStrictEqual ( ctx [ 1 ] , [ ] ) ;
80
87
@@ -98,7 +105,6 @@ test("Test Application's selectNextPerson", async () => {
98
105
app . getRandomPerson = orgsnp ;
99
106
rdperson = app . selectNextPerson ( ) ;
100
107
assert . strictEqual ( rdperson , null ) ;
101
- assert . strictEqual ( rdperson , null ) ;
102
108
assert . strictEqual ( app . selected . length , 2 ) ;
103
109
104
110
let cnt = 0 ;
@@ -113,18 +119,16 @@ test("Test Application's selectNextPerson", async () => {
113
119
rdperson = app . selectNextPerson ( ) ;
114
120
assert . strictEqual ( rdperson , 'EE' ) ;
115
121
assert . deepStrictEqual ( app . selected , [ 'JJ' , 'EE' ] ) ;
116
- fs . unlinkSync ( fn_ ) ;
122
+ originalUnlinkSync ( fn_ ) ;
117
123
} ) ;
118
124
119
125
test ( "Test Application's notifySelected" , async ( ) => {
120
-
121
126
const fn_ = 'name_list.txt' ;
122
127
const data_ = "JJ\nEE" ;
123
128
await writeFile ( fn_ , data_ , "utf-8" ) ;
124
129
125
130
const app = new Application ( ) ;
126
- let ctx = new Array ( [ ] , [ ] ) ;
127
- ctx = await app . getNames ( ) ;
131
+ let ctx = await app . getNames ( ) ;
128
132
assert . deepStrictEqual ( ctx [ 0 ] , [ "JJ" , "EE" ] ) ;
129
133
assert . deepStrictEqual ( ctx [ 1 ] , [ ] ) ;
130
134
@@ -155,11 +159,5 @@ test("Test Application's notifySelected", async () => {
155
159
assert . deepStrictEqual ( call . arguments , [ "EE" ] ) ;
156
160
assert . deepStrictEqual ( call . result , "Congrats, EE!" ) ;
157
161
assert . deepStrictEqual ( call . error , undefined ) ;
158
- fs . unlinkSync ( fn_ ) ;
162
+ originalUnlinkSync ( fn_ ) ;
159
163
} ) ;
160
-
161
- const { Application, MailSystem } = require ( './main' ) ;
162
-
163
- // TODO: write your tests here
164
- // Remember to use Stub, Mock, and Spy when necessary
165
-
0 commit comments