Files
privacy.sexy/tests/unit/infrastructure/Events/EventSource.spec.ts
undergroundwires 5f11c8d98f Migrate unit/integration tests to Vitest with Vite
As part of transition to Vue 3.0 and Vite (#230), this commit
facilitates the shift towards building rest of the application using
Vite. By doing so, it eliminates reliance on outdated Electron building
system that offered limited control, blocking desktop builds (#233).

Changes include:

- Introduce Vite with Vue 2.0 plugin for test execution.
- Remove `mocha`, `chai` and other related dependencies.
- Adjust test to Vitest syntax.
- Revise and update `tests.md` to document the changes.
- Add `@modyfi/vite-plugin-yaml` plugin to be able to use yaml file
  depended logic on test files, replacing previous webpack behavior.
- Fix failing tests that are revealed by Vitest due to unhandled errors
  and lack of assertments.
- Remove the test that depends on Vue CLI populating `process.env`.
- Use `jsdom` for unit test environment, adding it to dependency to
  `package.json` as project now depends on it and it was not specified
  even though `package-lock.json` included it.
2023-08-22 14:02:35 +02:00

91 lines
2.7 KiB
TypeScript

import {
describe, it, expect, beforeEach,
} from 'vitest';
import { EventHandler, IEventSource, IEventSubscription } from '@/infrastructure/Events/IEventSource';
import { EventSource } from '@/infrastructure/Events/EventSource';
describe('EventSource', () => {
class ObserverMock {
public readonly onReceiveCalls = new Array<number>();
public readonly callbacks = new Array<EventHandler<number>>();
public readonly subscription: IEventSubscription;
constructor(subject: IEventSource<number>) {
this.callbacks.push((arg) => this.onReceiveCalls.push(arg));
this.subscription = subject.on((arg) => this.callbacks.forEach((action) => action(arg)));
}
}
let sut: EventSource<number>;
beforeEach(() => {
sut = new EventSource();
});
describe('single observer', () => {
// arrange
let observer: ObserverMock;
beforeEach(() => {
observer = new ObserverMock(sut);
});
it('notify() executes the callback', () => {
// act
sut.notify(5);
// assert
expect(observer.onReceiveCalls).to.have.length(1);
});
it('notify() executes the callback with the payload', () => {
const expected = 5;
// act
sut.notify(expected);
// assert
expect(observer.onReceiveCalls).to.deep.equal([expected]);
});
it('notify() does not call callback when unsubscribed', () => {
// act
observer.subscription.unsubscribe();
sut.notify(5);
// assert
expect(observer.onReceiveCalls).to.have.lengthOf(0);
});
});
describe('multiple observers', () => {
// arrange
let observers: ObserverMock[];
beforeEach(() => {
observers = [
new ObserverMock(sut), new ObserverMock(sut),
new ObserverMock(sut), new ObserverMock(sut),
];
});
it('notify() should execute all callbacks', () => {
// act
sut.notify(5);
// assert
observers.forEach((observer) => {
expect(observer.onReceiveCalls).to.have.length(1);
});
});
it('notify() should execute all callbacks with payload', () => {
const expected = 5;
// act
sut.notify(expected);
// assert
observers.forEach((observer) => {
expect(observer.onReceiveCalls).to.deep.equal([expected]);
});
});
it('notify() executes in FIFO order', () => {
// arrange
const expectedSequence = [0, 1, 2, 3];
const actualSequence = new Array<number>();
for (let i = 0; i < observers.length; i++) {
observers[i].callbacks.push(() => actualSequence.push(i));
}
// act
sut.notify(5);
// assert
expect(actualSequence).to.deep.equal(expectedSequence);
});
});
});