Files
privacy.sexy/tests/unit/presentation/components/Shared/Modal/Hooks/UseAllTrueWatcher.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

164 lines
4.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { ref, nextTick } from 'vue';
import { useAllTrueWatcher } from '@/presentation/components/Shared/Modal/Hooks/UseAllTrueWatcher';
describe('useAllTrueWatcher', () => {
describe('onAllConditionsMet', () => {
it('triggers the callback when all conditions turn true', async () => {
// arrange
const condition1 = ref(false);
const condition2 = ref(false);
const { onAllConditionsMet } = useAllTrueWatcher(condition1, condition2);
let callbackCalled = false;
// act
onAllConditionsMet(() => {
callbackCalled = true;
});
condition1.value = true;
condition2.value = true;
await nextTick();
// assert
expect(callbackCalled).to.equal(true);
});
it('instantly triggers the callback if conditions are true on callback registration', async () => {
// arrange
const condition1 = ref(true);
const condition2 = ref(true);
const { onAllConditionsMet } = useAllTrueWatcher(condition1, condition2);
let callbackCalled = false;
// act
onAllConditionsMet(() => {
callbackCalled = true;
});
await nextTick();
// assert
expect(callbackCalled).to.equal(true);
});
it('does not trigger the callback unless all conditions are met', async () => {
// arrange
const condition1 = ref(true);
const condition2 = ref(false);
const { onAllConditionsMet } = useAllTrueWatcher(condition1, condition2);
let callbackCalled = false;
// act
onAllConditionsMet(() => {
callbackCalled = true;
});
await nextTick();
// assert
expect(callbackCalled).to.be.equal(false);
});
it('triggers all registered callbacks once all conditions are satisfied', async () => {
// arrange
const condition1 = ref(false);
const condition2 = ref(false);
const { onAllConditionsMet } = useAllTrueWatcher(condition1, condition2);
let callbackCount = 0;
// act
onAllConditionsMet(() => callbackCount++);
onAllConditionsMet(() => callbackCount++);
condition1.value = true;
condition2.value = true;
await nextTick();
// assert
expect(callbackCount).to.equal(2);
});
it('ensures each callback is invoked only once for a single condition set', async () => {
// arrange
const condition1 = ref(false);
const condition2 = ref(false);
const { onAllConditionsMet } = useAllTrueWatcher(condition1, condition2);
let callbackCount = 0;
// act
onAllConditionsMet(() => callbackCount++);
condition1.value = true;
condition2.value = true;
condition1.value = false;
condition1.value = true;
await nextTick();
// assert
expect(callbackCount).to.equal(1);
});
it('triggers the callback after conditions are sequentially met post-reset', async () => {
// arrange
const condition1 = ref(false);
const condition2 = ref(false);
const { onAllConditionsMet, resetAllConditions } = useAllTrueWatcher(condition1, condition2);
let callbackCalled = false;
// act
onAllConditionsMet(() => {
callbackCalled = true;
});
condition1.value = true;
resetAllConditions();
condition1.value = true;
condition2.value = true;
await nextTick();
// assert
expect(callbackCalled).to.equal(true);
});
it('avoids triggering the callback for single condition post-reset', async () => {
// arrange
const condition1 = ref(false);
const condition2 = ref(false);
const { onAllConditionsMet, resetAllConditions } = useAllTrueWatcher(condition1, condition2);
let callbackCalled = false;
// act
condition1.value = true;
condition2.value = true;
resetAllConditions();
onAllConditionsMet(() => {
callbackCalled = true;
});
condition1.value = true;
await nextTick();
// assert
expect(callbackCalled).to.equal(false);
});
});
describe('resetAllConditions', () => {
it('returns all conditions to their default false state', async () => {
// arrange
const condition1 = ref(true);
const condition2 = ref(true);
const { resetAllConditions } = useAllTrueWatcher(condition1, condition2);
// act
resetAllConditions();
await nextTick();
// assert
expect(condition1.value).to.be.equal(false);
expect(condition2.value).to.be.equal(false);
});
});
});