ed-safari/test/EDSM.test.ts

59 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2023-05-18 20:13:30 -07:00
import { expect, jest } from '@jest/globals';
import { EDSM } from '../src/models/EDSM';
import { ExtendedNavRouteSystem, System } from '../src/models/System';
import { mockEdsmResponses, mockObjects } from './mockData';
2023-05-17 10:52:26 -07:00
describe('EDSM', () => {
describe('connect()', () => {
it('should create new instance', () => {
expect(EDSM.connect()).toBeInstanceOf(EDSM);
});
it('should get previously created instance', () => {
const edsm = EDSM.connect();
expect(EDSM.connect()).toBeInstanceOf(EDSM);
});
});
describe('getSystemValue()', () => {
2023-05-18 20:13:30 -07:00
const mockFetch = (data?: { [i: string]: any }, ok: boolean = true) => {
2023-05-17 10:52:26 -07:00
global.fetch = jest.fn(() =>
Promise.resolve({
2023-05-18 20:13:30 -07:00
ok: ok,
2023-05-17 10:52:26 -07:00
json: () => Promise.resolve(data),
} as Response),
);
};
it('should get system info', async () => {
2023-05-18 20:13:30 -07:00
const system = new System(mockObjects.navRouteSystem as ExtendedNavRouteSystem);
const data = mockEdsmResponses.systemValue;
2023-05-17 10:52:26 -07:00
mockFetch(data);
const result = await EDSM.getSystemValue(system);
expect(result).toEqual(data);
});
it('should not get system info if system name is invalid', async () => {
const system = new System();
2023-05-18 20:13:30 -07:00
const data = {};
2023-05-17 10:52:26 -07:00
mockFetch({});
const result = await EDSM.getSystemValue(system);
expect(result).toEqual(data);
});
it('should return undefined if response is not ok', async () => {
const system = new System();
mockFetch({}, false);
const result = await EDSM.getSystemValue(system);
expect(result).toBeUndefined();
});
afterEach(() => {
jest.restoreAllMocks();
});
});
});