-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { instance } from '.'; | ||
import * as relayInfoUtils from './relayInfo'; | ||
import * as launchUtils from './launch'; | ||
import { RELAY_PKG_NAME } from './const'; | ||
|
||
jest.mock('fs'); | ||
jest.mock('./relayInfo'); | ||
jest.mock('./launch'); | ||
|
||
describe('instance', () => { | ||
it('returns relay info of existing relay instance', async () => { | ||
const relayInfo = { port: 1, pid: 1 }; | ||
(relayInfoUtils.readRelayInfo as jest.Mock).mockResolvedValueOnce( | ||
relayInfo, | ||
); | ||
|
||
await expect(instance()).resolves.toEqual(relayInfo); | ||
}); | ||
|
||
it('throws if local relay pkg not installed', async () => { | ||
jest | ||
.spyOn(relayInfoUtils, 'isRelayPkgInstalled') | ||
.mockResolvedValueOnce(false); | ||
|
||
await expect(instance()).rejects.toThrow( | ||
`To launch local relay (-l, --local flag), you should have ${RELAY_PKG_NAME} installed. No ${RELAY_PKG_NAME} dependency found in package.json`, | ||
); | ||
}); | ||
|
||
describe('launches relay instance if no existing relay instance', () => { | ||
beforeEach(() => { | ||
jest | ||
.spyOn(relayInfoUtils, 'isRelayPkgInstalled') | ||
.mockResolvedValueOnce(true); | ||
}); | ||
|
||
it('polls and returns launched relay instance info', async () => { | ||
const relayInfo = { port: 1, pid: 1 }; | ||
(relayInfoUtils.readRelayInfo as jest.Mock).mockResolvedValueOnce(false); | ||
(relayInfoUtils.pollRelayInfo as jest.Mock).mockResolvedValueOnce( | ||
relayInfo, | ||
); | ||
|
||
await expect(instance()).resolves.toEqual(relayInfo); | ||
// launch() is an empty mock | ||
expect(launchUtils.launch).toHaveBeenCalled(); | ||
}); | ||
|
||
it('throws if no launched relay instance info obtained', async () => { | ||
(relayInfoUtils.readRelayInfo as jest.Mock).mockResolvedValueOnce(false); | ||
(relayInfoUtils.pollRelayInfo as jest.Mock).mockResolvedValueOnce(false); | ||
|
||
await expect(instance()).rejects.toThrow( | ||
"Couldn't obtain Local Relay port and pid from PID file", | ||
); | ||
|
||
// launch() is an empty mock | ||
expect(launchUtils.launch).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |