Skip to content

Commit

Permalink
Use emitFakeCloseEvent util more
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed Mar 24, 2024
1 parent c86d247 commit 54818fb
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/completion-listener.spec.ts
Expand Up @@ -32,32 +32,31 @@ describe('listen', () => {
const result = createController().listen(commands).finally(finallyCallback);

// Emitting multiple close events to mimic calling command `kill/start` APIs.
commands[0].close.next(createFakeCloseEvent({ exitCode: 0 }));
commands[0].close.next(createFakeCloseEvent({ exitCode: 0 }));
commands[0].close.next(createFakeCloseEvent({ exitCode: 0 }));
emitFakeCloseEvent(commands[0]);
emitFakeCloseEvent(commands[0]);
emitFakeCloseEvent(commands[0]);

scheduler.flush();
// A broken implementantion will have called finallyCallback only after flushing promises
await flushPromises();
expect(finallyCallback).not.toHaveBeenCalled();

commands[1].close.next(createFakeCloseEvent({ exitCode: 0 }));
commands[2].close.next(createFakeCloseEvent({ exitCode: 0 }));
emitFakeCloseEvent(commands[1]);
emitFakeCloseEvent(commands[2]);

scheduler.flush();

await expect(result).resolves.toEqual(expect.anything());

expect(finallyCallback).toHaveBeenCalled();
});

it('takes last event emitted from each command', async () => {
const result = createController().listen(commands);

commands[0].close.next(createFakeCloseEvent({ exitCode: 0 }));
commands[0].close.next(createFakeCloseEvent({ exitCode: 1 }));
commands[1].close.next(createFakeCloseEvent({ exitCode: 0 }));
commands[2].close.next(createFakeCloseEvent({ exitCode: 0 }));
emitFakeCloseEvent(commands[0], { exitCode: 0 });
emitFakeCloseEvent(commands[0], { exitCode: 1 });
emitFakeCloseEvent(commands[1], { exitCode: 0 });
emitFakeCloseEvent(commands[2], { exitCode: 0 });

scheduler.flush();

Expand All @@ -68,18 +67,18 @@ describe('listen', () => {
const finallyCallback = jest.fn();
const result = createController().listen(commands).finally(finallyCallback);

commands[0].close.next(createFakeCloseEvent());
emitFakeCloseEvent(commands[0]);
commands[0].state = 'started';
commands[1].close.next(createFakeCloseEvent());
commands[2].close.next(createFakeCloseEvent());
emitFakeCloseEvent(commands[1]);
emitFakeCloseEvent(commands[2]);

scheduler.flush();
// A broken implementantion will have called finallyCallback only after flushing promises
await flushPromises();
expect(finallyCallback).not.toHaveBeenCalled();

commands[0].state = 'exited';
commands[0].close.next(createFakeCloseEvent());
emitFakeCloseEvent(commands[0]);
scheduler.flush();

await expect(result).resolves.toEqual(expect.anything());
Expand Down

0 comments on commit 54818fb

Please sign in to comment.