dyomedea/src/store/map.test.ts

70 lines
2.1 KiB
TypeScript

import _ from 'lodash';
import { initialMapState, reevaluateState } from './map';
//interface CustomMatchers<R = unknown> {
// isDeepEqual(received: any, target: any): R;
//}
//declare global {
// namespace jest {
// interface Expect extends CustomMatchers {}
// interface Matchers<R> extends CustomMatchers<R> {}
// interface InverseAsymmetricMatchers extends CustomMatchers {}
// }
//}
expect.extend({
isDeepEqual: (received: any, target: any) => {
const pass = _.isEqual(received, target);
if (pass) {
return { message: () => '', pass: true };
}
return {
message: () =>
`${JSON.stringify(received)} instead of\n ${JSON.stringify(target)}`,
pass: false,
};
},
isAlmostDeepEqual: (received: any, target: any, delta: number) => {
const pass = _.isEqualWith(received, target, (r: any, t: any) => {
if (typeof r !== 'number' || typeof t !== 'number') {
return undefined;
}
console.log(`r: ${r}, t:${t}, ${Math.abs(r - t) <= delta}`);
return Math.abs(r - t) <= delta;
});
if (pass) {
return { message: () => '', pass: true };
}
return {
message: () =>
`${JSON.stringify(received)} instead of\n ${JSON.stringify(target)}`,
pass: false,
};
},
});
describe('Our isAlmostDeepEqual matcher', () => {
test('compares correctly two numbers that are almost equals', () => {
expect({ x: 1.001 }).isAlmostDeepEqual({ x: 1 }, 1e-2);
});
});
describe('Map store methods', () => {
test('initialize its state', () => {
const state = _.cloneDeep(initialMapState);
expect(state.tiles.nb.x).not.toBe(0);
});
test('reevaluateState keeps the same values', () => {
const state = _.cloneDeep(initialMapState);
reevaluateState(state);
expect(state).isAlmostDeepEqual(initialMapState, 1e-7);
});
test('reevaluateState computes the right longitude after a shift 50 pixels left', () => {
const state = _.cloneDeep(initialMapState);
state.slippy.translation.x = state.slippy.translation.x - 50;
reevaluateState(state);
expect(state.scope.center.lon).not.toBe(77.5539501);
});
});