Add test for ServerStore deserializer

This commit is contained in:
Bill Thornton 2022-05-26 10:36:42 -04:00
parent 0a1c471d2a
commit 035881ed71
2 changed files with 42 additions and 7 deletions

View File

@ -10,6 +10,13 @@ import { v4 as uuidv4 } from 'uuid';
import ServerModel from '../models/ServerModel';
export const DESERIALIZER = data => data.map(server => {
// Migrate from old url format
// TODO: Remove migration in next minor release
const url = server.url.href || server.url;
return new ServerModel(server.id, new URL(url), server.info);
});
export default class ServerStore {
servers = []
@ -34,12 +41,7 @@ export default class ServerStore {
decorate(ServerStore, {
servers: [
format(data => data.map(value => {
// Migrate from old url format
// TODO: Remove migration in next minor release
const url = value.url.href || value.url;
return new ServerModel(value.id, new URL(url), value.info);
})),
format(DESERIALIZER),
observable
],
addServer: action,

View File

@ -6,7 +6,9 @@
import { URL } from 'url';
import ServerStore from '../ServerStore';
import ServerModel from '../../models/ServerModel';
import ServerStore, { DESERIALIZER } from '../ServerStore';
const mockFetchInfo = jest.fn();
jest.mock('../../models/ServerModel', () => {
@ -61,3 +63,34 @@ describe('ServerStore', () => {
expect(mockFetchInfo).toHaveBeenCalledTimes(2);
});
});
describe('DESERIALIZER', () => {
it('should deserialize to a list of ServerModels', () => {
const serialized = [
{
id: 'TEST1',
url: { href: 'https://1.example.com' },
info: null
},
{
id: 'TEST2',
url: 'https://2.example.com/',
info: null
}
];
const deserialized = DESERIALIZER(serialized);
expect(deserialized).toHaveLength(2);
expect(deserialized[0]).toBeInstanceOf(ServerModel);
expect(deserialized[0].id).toBe(serialized[0].id);
expect(deserialized[0].url).toBeInstanceOf(URL);
expect(deserialized[0].url.href).toBe('https://1.example.com/');
expect(deserialized[1]).toBeInstanceOf(ServerModel);
expect(deserialized[1].id).toBe(serialized[1].id);
expect(deserialized[1].url).toBeInstanceOf(URL);
expect(deserialized[1].url.href).toBe('https://2.example.com/');
});
});