Files
privacy.sexy/tests/unit/presentation/components/Shared/Modal/Hooks/ScrollLock/UseLockBodyBackgroundScroll.spec.ts
undergroundwires ae0165f1fe Ensure tests do not log warning or errors
This commit increases strictnes of tests by failing on tests (even
though they pass) if `console.warn` or `console.error` is used. This is
used to fix warning outputs from Vue, cleaning up test output and
preventing potential issues with tests.

This commit fixes all of the failing tests, including refactoring in
code to make them more testable through injecting Vue lifecycle
hook function stubs. This removes `shallowMount`ing done on places,
improving the speed of executing unit tests. It also reduces complexity
and increases maintainability by removing `@vue/test-utils` dependency
for these tests.

Changes:

- Register global hook for all tests to fail if console.error or
  console.warn is being used.
- Fix all issues with failing tests.
- Create test helper function for running code in a wrapper component to
  run code in reliable/unified way to surpress Vue warnings about code
  not running inside `setup`.
2024-07-23 16:08:04 +02:00

136 lines
4.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { ref, nextTick } from 'vue';
import { useLockBodyBackgroundScroll } from '@/presentation/components/Shared/Modal/Hooks/ScrollLock/UseLockBodyBackgroundScroll';
import type { ScrollDomStateAccessor } from '@/presentation/components/Shared/Modal/Hooks/ScrollLock/ScrollDomStateAccessor';
import { executeInComponentSetupContext } from '@tests/shared/Vue/ExecuteInComponentSetupContext';
import { DomStateChangeTestScenarios } from './DomStateChangeTestScenarios';
describe('useLockBodyBackgroundScroll', () => {
describe('initialization', () => {
describe('activates scroll lock when initially active', () => {
itEachScrollBlockEffect(async (dom) => {
// arrange
const isInitiallyActive = true;
// act
mountWrapperComponent(isInitiallyActive, dom);
await nextTick();
});
});
it('maintains initial styles when initially inactive', async () => {
// arrange
const isInitiallyActive = false;
// act
const { initialDomState, actualDomState } = mountWrapperComponent(isInitiallyActive);
await nextTick();
// assert
expect(actualDomState).to.deep.equal(initialDomState);
});
});
describe('toggling scroll lock', () => {
describe('enforces scroll lock when activated', async () => {
itEachScrollBlockEffect(async (dom) => {
// arrange
const isInitiallyActive = false;
const { isActive } = mountWrapperComponent(isInitiallyActive, dom);
// act
isActive.value = true;
await nextTick();
});
});
it('reverts to initial styles when deactivated', async () => {
// arrange
const isInitiallyActive = true;
const {
isActive, initialDomState, actualDomState,
} = mountWrapperComponent(isInitiallyActive);
// act
isActive.value = false;
await nextTick();
// assert
expect(actualDomState).to.deep.equal(initialDomState);
});
});
it('restores original styles on unmount', async () => {
// arrange
const isInitiallyActive = true;
const {
component, initialDomState, actualDomState,
} = mountWrapperComponent(isInitiallyActive);
// act
component.unmount();
await nextTick();
// assert
expect(actualDomState).to.deep.equal(initialDomState);
});
});
function mountWrapperComponent(
initialIsActiveValue: boolean,
dom?: ScrollDomStateAccessor,
) {
const actualDomState = dom ?? createMockDomStateAccessor();
const initialDomState = { ...actualDomState };
const isActive = ref(initialIsActiveValue);
const component = executeInComponentSetupContext({
setupCallback: () => {
useLockBodyBackgroundScroll(isActive, actualDomState);
},
disableAutoUnmount: true,
});
return {
component, isActive, initialDomState, actualDomState,
};
}
function itEachScrollBlockEffect(act: (dom: ScrollDomStateAccessor) => Promise<void>) {
DomStateChangeTestScenarios.forEach((m) => {
const description = m.description ? ` (${m.description})` : '';
it(`handles '${m.propertyName}'${description}`, async () => {
// arrange
const dom = createMockDomStateAccessor();
const initialDom = { ...dom };
if (m.prepare) {
m.prepare(dom);
}
// act
await act(dom);
// assert
const expectedValue = m.getExpectedValueOnBlock(initialDom, dom);
const actualValue = dom[m.propertyName];
expect(actualValue).to.equal(expectedValue);
});
});
}
function createMockDomStateAccessor(): ScrollDomStateAccessor {
return {
bodyStyleOverflowX: '',
bodyStyleOverflowY: '',
htmlScrollLeft: 0,
htmlScrollTop: 0,
bodyStyleLeft: '',
bodyStyleTop: '',
bodyStylePosition: '',
bodyStyleWidth: '',
bodyStyleHeight: '',
bodyComputedMarginLeft: '',
bodyComputedMarginRight: '',
bodyComputedMarginTop: '',
bodyComputedMarginBottom: '',
htmlScrollWidth: 0,
htmlScrollHeight: 0,
htmlClientWidth: 0,
htmlClientHeight: 0,
htmlOffsetHeight: 0,
htmlOffsetWidth: 0,
};
}