From b69600093aa8c453a4e1b48acb3324febfd474b0 Mon Sep 17 00:00:00 2001 From: naumov Date: Tue, 26 Sep 2023 16:26:39 +0200 Subject: [PATCH] CB-3908 transform mock data to getters --- .../src/Resource/CachedDataResource.test.ts | 19 +++++++++---------- .../src/Resource/CachedMapResource.test.ts | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/webapp/packages/core-sdk/src/Resource/CachedDataResource.test.ts b/webapp/packages/core-sdk/src/Resource/CachedDataResource.test.ts index 7022435f9c..090cc5c880 100644 --- a/webapp/packages/core-sdk/src/Resource/CachedDataResource.test.ts +++ b/webapp/packages/core-sdk/src/Resource/CachedDataResource.test.ts @@ -7,14 +7,13 @@ */ import { CachedDataResource } from './CachedDataResource'; -interface ILoaderData { +interface IEntityData { id: string; value: number; } -const DEFAULT_STATE: () => ILoaderData[] = () => []; - -const LOADER_DATA_MOCK: ILoaderData[] = [ +const DEFAULT_STATE_GETTER: () => IEntityData[] = () => []; +const DATA_MOCK_GETTER: () => IEntityData[] = () => [ { id: '1', value: 1, @@ -25,17 +24,17 @@ const LOADER_DATA_MOCK: ILoaderData[] = [ }, ]; -async function fetchMock(): Promise { +async function fetchMock(): Promise { return new Promise(resolve => { setTimeout(() => { - resolve(LOADER_DATA_MOCK); + resolve(DATA_MOCK_GETTER()); }, 1); }); } -class TestDataResource extends CachedDataResource { +class TestDataResource extends CachedDataResource { constructor() { - super(DEFAULT_STATE); + super(DEFAULT_STATE_GETTER); } protected async loader() { @@ -56,12 +55,12 @@ describe('CachedDataResource', () => { }); test('should initialize with the default state', () => { - expect(dataResource.data).toEqual(DEFAULT_STATE()); + expect(dataResource.data).toEqual(DEFAULT_STATE_GETTER()); }); test('should load data', async () => { await dataResource.load(); - expect(dataResource.data).toEqual(LOADER_DATA_MOCK); + expect(dataResource.data).toEqual(DATA_MOCK_GETTER()); }); test('should be able to outdate the data', () => { diff --git a/webapp/packages/core-sdk/src/Resource/CachedMapResource.test.ts b/webapp/packages/core-sdk/src/Resource/CachedMapResource.test.ts index abb89f7d66..7981a80274 100644 --- a/webapp/packages/core-sdk/src/Resource/CachedMapResource.test.ts +++ b/webapp/packages/core-sdk/src/Resource/CachedMapResource.test.ts @@ -18,7 +18,7 @@ interface IEntityData { const ERROR_ITEM_ID = 'error'; -const DATA_MOCK: IEntityData[] = [ +const DATA_MOCK_GETTER: () => IEntityData[] = () => [ { id: '1', value: 1, @@ -34,9 +34,11 @@ const DATA_MOCK: IEntityData[] = [ ]; const TEST_ERROR_MESSAGE = 'Test error'; -const DEFAULT_STATE = () => new Map(); +const DEFAULT_STATE_GETTER = () => new Map(); async function fetchMock(key: ResourceKey | undefined): Promise { + const data = DATA_MOCK_GETTER(); + return new Promise((resolve, reject) => { setTimeout(() => { if (key) { @@ -44,12 +46,12 @@ async function fetchMock(key: ResourceKey | undefined): Promise d.id === key); - if (data) { - resolve([data]); + const item = data.find(d => d.id === key); + if (item) { + resolve([item]); } } else { - resolve(DATA_MOCK); + resolve(data); } }, 1); }); @@ -57,7 +59,7 @@ async function fetchMock(key: ResourceKey | undefined): Promise { constructor() { - super(DEFAULT_STATE); + super(DEFAULT_STATE_GETTER); } protected async loader(key: ResourceKey) { @@ -83,7 +85,7 @@ describe('CachedMapResource', () => { }); test('should initialize with the initial value', () => { - expect(toJS(mapResource.data)).toEqual(DEFAULT_STATE()); + expect(toJS(mapResource.data)).toEqual(DEFAULT_STATE_GETTER()); }); test('should return all entries', () => {