|
1 | 1 | import { readFile, writeFile } from 'fs'; |
2 | | -import { addDomainToHosts, removeDomainFromHosts } from '../hosts-file'; |
| 2 | +import { addDomainToHosts, removeDomainFromHosts, replaceDomainInHosts } from '../hosts-file'; |
3 | 3 |
|
4 | 4 | const readFileCallbackMock = jest.fn(); |
5 | 5 |
|
@@ -191,4 +191,94 @@ describe( 'hosts-file', () => { |
191 | 191 | await expect( removeDomainFromHosts( 'test.wp.cloud' ) ).rejects.toThrow( 'Read error' ); |
192 | 192 | } ); |
193 | 193 | } ); |
| 194 | + |
| 195 | + // Generate a few unit tests for the replaceDomainInHosts function |
| 196 | + describe( 'replaceDomainInHosts', () => { |
| 197 | + it( 'should replace an existing domain with a new domain', async () => { |
| 198 | + readFileCallbackMock.mockResolvedValueOnce( sampleHostsContent ); |
| 199 | + |
| 200 | + await replaceDomainInHosts( 'foo.wp.cloud', 'new-domain.wp.cloud', 8002 ); |
| 201 | + |
| 202 | + expect( readFile ).toHaveBeenCalled(); |
| 203 | + expect( writeFile ).toHaveBeenCalled(); |
| 204 | + |
| 205 | + const newContent = ( writeFile as unknown as jest.Mock ).mock.calls[ 0 ][ 1 ]; |
| 206 | + |
| 207 | + expect( newContent ).toEqual( |
| 208 | + `127.0.0.1 localhost |
| 209 | +::1 localhost |
| 210 | +
|
| 211 | +# Some comment |
| 212 | +
|
| 213 | +# BEGIN WordPress Studio |
| 214 | +127.0.0.1 bar.wp.cloud # Port 8001 |
| 215 | +127.0.0.1 new-domain.wp.cloud # Port 8002 |
| 216 | +# END WordPress Studio |
| 217 | +
|
| 218 | +# Other entries |
| 219 | +192.168.1.1 router` |
| 220 | + ); |
| 221 | + } ); |
| 222 | + |
| 223 | + it( 'should add a new domain if old domain is undefined', async () => { |
| 224 | + readFileCallbackMock.mockResolvedValueOnce( sampleHostsContent ); |
| 225 | + |
| 226 | + await replaceDomainInHosts( undefined, 'new-domain.wp.cloud', 8002 ); |
| 227 | + |
| 228 | + expect( readFile ).toHaveBeenCalled(); |
| 229 | + expect( writeFile ).toHaveBeenCalled(); |
| 230 | + |
| 231 | + const newContent = ( writeFile as unknown as jest.Mock ).mock.calls[ 0 ][ 1 ]; |
| 232 | + |
| 233 | + expect( newContent ).toEqual( |
| 234 | + `127.0.0.1 localhost |
| 235 | +::1 localhost |
| 236 | +
|
| 237 | +# Some comment |
| 238 | +
|
| 239 | +# BEGIN WordPress Studio |
| 240 | +127.0.0.1 foo.wp.cloud # Port 8000 |
| 241 | +127.0.0.1 bar.wp.cloud # Port 8001 |
| 242 | +127.0.0.1 new-domain.wp.cloud # Port 8002 |
| 243 | +# END WordPress Studio |
| 244 | +
|
| 245 | +# Other entries |
| 246 | +192.168.1.1 router` |
| 247 | + ); |
| 248 | + } ); |
| 249 | + |
| 250 | + it( 'should remove the old domain if new domain is undefined', async () => { |
| 251 | + readFileCallbackMock.mockResolvedValueOnce( sampleHostsContent ); |
| 252 | + |
| 253 | + await replaceDomainInHosts( 'foo.wp.cloud', undefined, 8000 ); |
| 254 | + |
| 255 | + expect( readFile ).toHaveBeenCalled(); |
| 256 | + expect( writeFile ).toHaveBeenCalled(); |
| 257 | + |
| 258 | + const newContent = ( writeFile as unknown as jest.Mock ).mock.calls[ 0 ][ 1 ]; |
| 259 | + |
| 260 | + expect( newContent ).toEqual( |
| 261 | + `127.0.0.1 localhost |
| 262 | +::1 localhost |
| 263 | +
|
| 264 | +# Some comment |
| 265 | +
|
| 266 | +# BEGIN WordPress Studio |
| 267 | +127.0.0.1 bar.wp.cloud # Port 8001 |
| 268 | +# END WordPress Studio |
| 269 | +
|
| 270 | +# Other entries |
| 271 | +192.168.1.1 router` |
| 272 | + ); |
| 273 | + } ); |
| 274 | + |
| 275 | + it( 'should not modify the hosts file if old and new domains are the same', async () => { |
| 276 | + readFileCallbackMock.mockResolvedValueOnce( sampleHostsContent ); |
| 277 | + |
| 278 | + await replaceDomainInHosts( 'foo.wp.cloud', 'foo.wp.cloud', 8000 ); |
| 279 | + |
| 280 | + expect( readFile ).not.toHaveBeenCalled(); |
| 281 | + expect( writeFile ).not.toHaveBeenCalled(); |
| 282 | + } ); |
| 283 | + } ); |
194 | 284 | } ); |
0 commit comments