Skip to content

Commit

Permalink
Add another test back
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexxeh committed Nov 10, 2021
1 parent 9b293e6 commit a0f1f69
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions packages/sdk-cli/src/models/localRelay/index.test.ts
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();
});
});
});

0 comments on commit a0f1f69

Please sign in to comment.