fix tests

This commit is contained in:
Rene
2022-06-10 11:52:45 +02:00
parent 35868511ff
commit a40a8802ec
6 changed files with 541 additions and 228 deletions
@@ -15,21 +15,24 @@ const createUpdater = <T, C = unknown>(updaterReturn: (i: number) => T) => {
describe('cache', () => {
test('creates and updates cache', () => {
const [fn, updater] = createUpdater((i) => `${i}`);
const { _update, _current } = createCache<string>(updater);
const _initialValue = '';
const [updateCache, getCurrentCache] = createCache<string>(updater, {
_initialValue,
});
let { _value, _previous, _changed } = _update();
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toBe('1');
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
let [value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, _initialValue, undefined);
expect(value).toBe('1');
expect(previous).toBe(_initialValue);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update());
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(fn).toHaveBeenLastCalledWith(undefined, '1', undefined);
expect(_value).toBe('2');
expect(_previous).toBe('1');
expect(_changed).toBe(true);
[value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, '1', _initialValue);
expect(value).toBe('2');
expect(previous).toBe('1');
expect(changed).toBe(true);
});
describe('context', () => {
@@ -38,51 +41,52 @@ describe('cache', () => {
test: string;
even: number;
}
const _initialValue = false;
const updateFn = jest.fn();
const updater = (context?: ContextObj, current?: boolean, previous?: boolean) => {
updateFn(context, current, previous);
return context!.test === 'test' || context!.even % 2 === 0;
};
const { _update, _current } = createCache(updater);
const [updateCache, getCurrentCache] = createCache(updater, { _initialValue });
const firstCtx = { test: 'test', even: 2 };
let { _value, _previous, _changed } = _update(0, firstCtx);
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(updateFn).toHaveBeenLastCalledWith(firstCtx, undefined, undefined);
expect(_value).toBe(true);
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
expect({ _value, _previous, _changed: false }).toEqual(_current());
let [value, changed, previous] = updateCache(0, firstCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(updateFn).toHaveBeenLastCalledWith(firstCtx, _initialValue, undefined);
expect(value).toBe(true);
expect(previous).toBe(_initialValue);
expect(changed).toBe(true);
expect([value, false, previous]).toEqual(getCurrentCache());
({ _value, _previous, _changed } = _update(0, firstCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(updateFn).toHaveBeenLastCalledWith(firstCtx, true, undefined);
expect(_value).toBe(true);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
[value, changed, previous] = updateCache(0, firstCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(updateFn).toHaveBeenLastCalledWith(firstCtx, true, _initialValue);
expect(value).toBe(true);
expect(previous).toBe(_initialValue);
expect(changed).toBe(false);
const scndCtx = { test: 'nah', even: 1 };
({ _value, _previous, _changed } = _update(0, scndCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(updateFn).toHaveBeenLastCalledWith(scndCtx, true, undefined);
expect(_value).toBe(false);
expect(_previous).toBe(true);
expect(_changed).toBe(true);
[value, changed, previous] = updateCache(0, scndCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(updateFn).toHaveBeenLastCalledWith(scndCtx, true, _initialValue);
expect(value).toBe(false);
expect(previous).toBe(true);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update(0, scndCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache(0, scndCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(updateFn).toHaveBeenLastCalledWith(scndCtx, false, true);
expect(_value).toBe(false);
expect(_previous).toBe(true);
expect(_changed).toBe(false);
expect(value).toBe(false);
expect(previous).toBe(true);
expect(changed).toBe(false);
({ _value, _previous, _changed } = _update(true, scndCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache(true, scndCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(updateFn).toHaveBeenLastCalledWith(scndCtx, false, true);
expect(_value).toBe(false);
expect(_previous).toBe(false);
expect(_changed).toBe(true);
expect(value).toBe(false);
expect(previous).toBe(false);
expect(changed).toBe(true);
});
test('creates and updates cache with context shorthand', () => {
@@ -90,151 +94,170 @@ describe('cache', () => {
test: string;
even: number;
}
const { _update } = createCache<ContextObj, ContextObj>(0);
const _initialValue = undefined;
const firstCtx = { test: 'test', even: 2 };
const [_update] = createCache<ContextObj | undefined, ContextObj>(0, {
_initialValue,
});
let { _value, _previous, _changed } = _update(0, firstCtx);
expect(_value).toBe(firstCtx);
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
let [value, changed, previous] = _update(0, firstCtx);
expect(value).toBe(firstCtx);
expect(previous).toBe(undefined);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update(0, firstCtx));
expect(_value).toBe(firstCtx);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
[value, changed, previous] = _update(0, firstCtx);
expect(value).toBe(firstCtx);
expect(previous).toBe(undefined);
expect(changed).toBe(false);
const scndCtx = { test: 'nah', even: 1 };
({ _value, _previous, _changed } = _update(0, scndCtx));
expect(_value).toBe(scndCtx);
expect(_previous).toBe(firstCtx);
expect(_changed).toBe(true);
[value, changed, previous] = _update(0, scndCtx);
expect(value).toBe(scndCtx);
expect(previous).toBe(firstCtx);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update(0, scndCtx));
expect(_value).toBe(scndCtx);
expect(_previous).toBe(firstCtx);
expect(_changed).toBe(false);
[value, changed, previous] = _update(0, scndCtx);
expect(value).toBe(scndCtx);
expect(previous).toBe(firstCtx);
expect(changed).toBe(false);
({ _value, _previous, _changed } = _update(true, scndCtx));
expect(_value).toBe(scndCtx);
expect(_previous).toBe(scndCtx);
expect(_changed).toBe(true);
[value, changed, previous] = _update(true, scndCtx);
expect(value).toBe(scndCtx);
expect(previous).toBe(scndCtx);
expect(changed).toBe(true);
});
});
describe('equal', () => {
test('with equal always true', () => {
const [fn, updater] = createUpdater((i) => i);
const { _update, _current } = createCache<number>(updater, { _equal: () => true });
const [updateCache, getCurrentCache] = createCache<number | undefined>(updater, {
_initialValue: undefined,
_equal: () => true,
});
let { _value, _previous, _changed } = _update();
expect({ _value, _previous, _changed: false }).toEqual(_current());
let [value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toBe(undefined);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
expect(value).toBe(undefined);
expect(previous).toBe(undefined);
expect(changed).toBe(false);
({ _value, _previous, _changed } = _update());
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toBe(undefined);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
expect(value).toBe(undefined);
expect(previous).toBe(undefined);
expect(changed).toBe(false);
});
test('with equal always false', () => {
const [fn, updater] = createUpdater(() => 1);
const { _update, _current } = createCache<number>(updater, { _equal: () => false });
const [updateCache, getCurrentCache] = createCache<number | undefined>(updater, {
_initialValue: undefined,
_equal: () => false,
});
let { _value, _previous, _changed } = _update();
expect({ _value, _previous, _changed: false }).toEqual(_current());
let [value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toBe(1);
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
expect(value).toBe(1);
expect(previous).toBe(undefined);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update());
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, 1, undefined);
expect(_value).toBe(1);
expect(_previous).toBe(1);
expect(_changed).toBe(true);
expect(value).toBe(1);
expect(previous).toBe(1);
expect(changed).toBe(true);
});
test('with object equal', () => {
const obj = { a: -1, b: -1 };
const [fn, updater] = createUpdater((i) => ({ a: i, b: i + 1 }));
const { _update } = createCache<typeof obj>(updater, { _equal: (a, b) => a?.a === b?.a && a?.b === b?.b });
const [updateCache] = createCache<typeof obj | undefined>(updater, {
_initialValue: undefined,
_equal: (a, b) => a?.a === b?.a && a?.b === b?.b,
});
let { _value, _previous, _changed } = _update();
let [value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toEqual({ a: 1, b: 2 });
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
expect(value).toEqual({ a: 1, b: 2 });
expect(previous).toBe(undefined);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update());
[value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, { a: 1, b: 2 }, undefined);
expect(_value).toEqual({ a: 2, b: 3 });
expect(_previous).toEqual({ a: 1, b: 2 });
expect(_changed).toBe(true);
expect(value).toEqual({ a: 2, b: 3 });
expect(previous).toEqual({ a: 1, b: 2 });
expect(changed).toBe(true);
});
});
describe('inital value', () => {
test('creates and updates cache with initialValue', () => {
const [fn, updater] = createUpdater((i) => i);
const { _update, _current } = createCache<number>(updater, { _initialValue: 0 });
const [updateCache, getCurrentCache] = createCache<number>(updater, { _initialValue: 0 });
let { _value, _previous, _changed } = _update();
expect({ _value, _previous, _changed: false }).toEqual(_current());
let [value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, 0, undefined);
expect(_value).toBe(1);
expect(_previous).toBe(0);
expect(_changed).toBe(true);
expect(value).toBe(1);
expect(previous).toBe(0);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update());
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, 1, 0);
expect(_value).toBe(2);
expect(_previous).toBe(1);
expect(_changed).toBe(true);
expect(value).toBe(2);
expect(previous).toBe(1);
expect(changed).toBe(true);
});
test('creates and updates cache with initialValue and equal', () => {
const obj = { a: -1, b: -1 };
const [fn, updater] = createUpdater((i) => ({ a: i, b: i + 1 }));
const { _update } = createCache<typeof obj>(updater, { _initialValue: obj, _equal: (a, b) => a?.a === b?.a && a?.b === b?.b });
const [updateCache] = createCache<typeof obj>(updater, {
_initialValue: obj,
_equal: (a, b) => a?.a === b?.a && a?.b === b?.b,
});
let { _value, _previous, _changed } = _update();
let [value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, obj, undefined);
expect(_value).toEqual({ a: 1, b: 2 });
expect(_previous).toBe(obj);
expect(_changed).toBe(true);
expect(value).toEqual({ a: 1, b: 2 });
expect(previous).toBe(obj);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update());
[value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, { a: 1, b: 2 }, obj);
expect(_value).toEqual({ a: 2, b: 3 });
expect(_previous).toEqual({ a: 1, b: 2 });
expect(_changed).toBe(true);
expect(value).toEqual({ a: 2, b: 3 });
expect(previous).toEqual({ a: 1, b: 2 });
expect(changed).toBe(true);
});
});
describe('always update values', () => {
test('creates and updates cache with alwaysUpdateValues and equal always true', () => {
const [fn, updater] = createUpdater((i) => i);
const { _update } = createCache<number>(updater, { _alwaysUpdateValues: true, _equal: () => true });
const [updateCache] = createCache<number | undefined>(updater, {
_initialValue: undefined,
_alwaysUpdateValues: true,
_equal: () => true,
});
let { _value, _previous, _changed } = _update();
let [value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toBe(1);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
expect(value).toBe(1);
expect(previous).toBe(undefined);
expect(changed).toBe(false);
({ _value, _previous, _changed } = _update());
[value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, 1, undefined);
expect(_value).toBe(2);
expect(_previous).toBe(1);
expect(_changed).toBe(false);
expect(value).toBe(2);
expect(previous).toBe(1);
expect(changed).toBe(false);
});
test('creates and updates cache with context shorthand and alwaysUpdateValues', () => {
@@ -242,112 +265,119 @@ describe('cache', () => {
test: string;
even: number;
}
const { _update, _current } = createCache<ContextObj, ContextObj>(0, { _alwaysUpdateValues: true });
const [updateCache, getCurrentCache] = createCache<ContextObj | undefined, ContextObj>(0, {
_initialValue: undefined,
_alwaysUpdateValues: true,
});
const firstCtx = { test: 'test', even: 2 };
let { _value, _previous, _changed } = _update(0, firstCtx);
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(_value).toBe(firstCtx);
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
let [value, changed, previous] = updateCache(0, firstCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(value).toBe(firstCtx);
expect(previous).toBe(undefined);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update(0, firstCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(_value).toBe(firstCtx);
expect(_previous).toBe(firstCtx);
expect(_changed).toBe(false);
[value, changed, previous] = updateCache(0, firstCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(value).toBe(firstCtx);
expect(previous).toBe(firstCtx);
expect(changed).toBe(false);
const scndCtx = { test: 'nah', even: 1 };
({ _value, _previous, _changed } = _update(0, scndCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(_value).toBe(scndCtx);
expect(_previous).toBe(firstCtx);
expect(_changed).toBe(true);
[value, changed, previous] = updateCache(0, scndCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(value).toBe(scndCtx);
expect(previous).toBe(firstCtx);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update(0, scndCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(_value).toBe(scndCtx);
expect(_previous).toBe(scndCtx);
expect(_changed).toBe(false);
[value, changed, previous] = updateCache(0, scndCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(value).toBe(scndCtx);
expect(previous).toBe(scndCtx);
expect(changed).toBe(false);
({ _value, _previous, _changed } = _update(true, scndCtx));
expect({ _value, _previous, _changed: false }).toEqual(_current());
expect(_value).toBe(scndCtx);
expect(_previous).toBe(scndCtx);
expect(_changed).toBe(true);
[value, changed, previous] = updateCache(true, scndCtx);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(value).toBe(scndCtx);
expect(previous).toBe(scndCtx);
expect(changed).toBe(true);
});
});
describe('constant', () => {
test('updates constant initially without intial value', () => {
const [fn, updater] = createUpdater(() => true);
const { _update, _current } = createCache<boolean>(updater);
const [updateCache, getCurrentCache] = createCache<boolean | undefined>(updater, {
_initialValue: undefined,
});
let { _value, _previous, _changed } = _update();
expect({ _value, _previous, _changed: false }).toEqual(_current());
let [value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toBe(true);
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
expect(value).toBe(true);
expect(previous).toBe(undefined);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update());
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, true, undefined);
expect(_value).toBe(true);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
expect(value).toBe(true);
expect(previous).toBe(undefined);
expect(changed).toBe(false);
});
test('doesnt update constant with initial value', () => {
const obj = { constant: true };
const [fn, updater] = createUpdater(() => obj);
const { _update } = createCache<typeof obj>(updater, { _initialValue: obj });
const [updateCache] = createCache<typeof obj>(updater, { _initialValue: obj });
let { _value, _previous, _changed } = _update();
let [value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, obj, undefined);
expect(_value).toBe(obj);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
expect(value).toBe(obj);
expect(previous).toBe(undefined);
expect(changed).toBe(false);
({ _value, _previous, _changed } = _update());
[value, changed, previous] = updateCache();
expect(fn).toHaveBeenLastCalledWith(undefined, obj, undefined);
expect(_value).toBe(obj);
expect(_previous).toBe(undefined);
expect(_changed).toBe(false);
expect(value).toBe(obj);
expect(previous).toBe(undefined);
expect(changed).toBe(false);
});
test('updates constant with force', () => {
const [fn, updater] = createUpdater(() => 'constant');
const { _update, _current } = createCache<string>(updater);
const [updateCache, getCurrentCache] = createCache<string | undefined>(updater, {
_initialValue: undefined,
});
let { _value, _previous, _changed } = _update();
expect({ _value, _previous, _changed: false }).toEqual(_current());
let [value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, undefined, undefined);
expect(_value).toBe('constant');
expect(_previous).toBe(undefined);
expect(_changed).toBe(true);
expect(value).toBe('constant');
expect(previous).toBe(undefined);
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update(true));
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache(true);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, 'constant', undefined);
expect(_value).toBe('constant');
expect(_previous).toBe('constant');
expect(_changed).toBe(true);
expect(value).toBe('constant');
expect(previous).toBe('constant');
expect(changed).toBe(true);
({ _value, _previous, _changed } = _update(false));
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache(false);
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, 'constant', 'constant');
expect(_value).toBe('constant');
expect(_previous).toBe('constant');
expect(_changed).toBe(false);
expect(value).toBe('constant');
expect(previous).toBe('constant');
expect(changed).toBe(false);
({ _value, _previous, _changed } = _update());
expect({ _value, _previous, _changed: false }).toEqual(_current());
[value, changed, previous] = updateCache();
expect([value, false, previous]).toEqual(getCurrentCache());
expect(fn).toHaveBeenLastCalledWith(undefined, 'constant', 'constant');
expect(_value).toBe('constant');
expect(_previous).toBe('constant');
expect(_changed).toBe(false);
expect(value).toBe('constant');
expect(previous).toBe('constant');
expect(changed).toBe(false);
});
});
});
@@ -1,14 +1,12 @@
import 'jest-playwright-preset';
import 'expect-playwright';
// @ts-ignore
import { playwrightRollup } from '@/playwright/rollup';
import { test } from '@playwright/test';
import { Environment } from 'environment';
import url from './.build/build.html';
describe('Environment', () => {
beforeAll(async () => {
await page.goto(url);
});
playwrightRollup();
test('page should be titled "Environment"', async () => {
test.describe('Environment', () => {
test('page should be titled "Environment"', async ({ page }) => {
// @ts-ignore
const a: Environment = await page.evaluate(() => window.environment.envInstance);
console.log(a);