Files
privacy.sexy/tests/integration/presentation/electron/preload/WindowVariablesProvider.spec.ts
undergroundwires 949fac1a7c Refactor to enforce strictNullChecks
This commit applies `strictNullChecks` to the entire codebase to improve
maintainability and type safety. Key changes include:

- Remove some explicit null-checks where unnecessary.
- Add necessary null-checks.
- Refactor static factory functions for a more functional approach.
- Improve some test names and contexts for better debugging.
- Add unit tests for any additional logic introduced.
- Refactor `createPositionFromRegexFullMatch` to its own function as the
  logic is reused.
- Prefer `find` prefix on functions that may return `undefined` and
  `get` prefix for those that always return a value.
2023-11-12 22:54:00 +01:00

36 lines
1.2 KiB
TypeScript

import { it, describe, expect } from 'vitest';
import { provideWindowVariables } from '@/presentation/electron/preload/WindowVariablesProvider';
describe('WindowVariablesProvider', () => {
describe('provideWindowVariables', () => {
describe('conforms to Electron\'s context bridging requirements', () => {
// https://www.electronjs.org/docs/latest/api/context-bridge
const variables = provideWindowVariables();
Object.entries(variables).forEach(([key, value]) => {
it(`\`${key}\` conforms to allowed types for context bridging`, () => {
expect(checkAllowedType(value)).to.equal(true);
});
});
});
});
});
function checkAllowedType(value: unknown) {
const type = typeof value;
if (['string', 'number', 'boolean', 'function'].includes(type)) {
return true;
}
if (Array.isArray(value)) {
return value.every(checkAllowedType);
}
if (type === 'object' && value !== null && value !== undefined) {
return (
// Every key should be a string
Object.keys(value).every((key) => typeof key === 'string')
// Every value should be of allowed type
&& Object.values(value).every(checkAllowedType)
);
}
return false;
}