Skip to content

Commit

Permalink
CB-3908 transform mock data to getters
Browse files Browse the repository at this point in the history
  • Loading branch information
devnaumov committed Sep 26, 2023
1 parent 8f2d1c0 commit b696000
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
19 changes: 9 additions & 10 deletions webapp/packages/core-sdk/src/Resource/CachedDataResource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,17 +24,17 @@ const LOADER_DATA_MOCK: ILoaderData[] = [
},
];

async function fetchMock(): Promise<ILoaderData[]> {
async function fetchMock(): Promise<IEntityData[]> {
return new Promise(resolve => {
setTimeout(() => {
resolve(LOADER_DATA_MOCK);
resolve(DATA_MOCK_GETTER());
}, 1);
});
}

class TestDataResource extends CachedDataResource<ILoaderData[]> {
class TestDataResource extends CachedDataResource<IEntityData[]> {
constructor() {
super(DEFAULT_STATE);
super(DEFAULT_STATE_GETTER);
}

protected async loader() {
Expand All @@ -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', () => {
Expand Down
18 changes: 10 additions & 8 deletions webapp/packages/core-sdk/src/Resource/CachedMapResource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface IEntityData {

const ERROR_ITEM_ID = 'error';

const DATA_MOCK: IEntityData[] = [
const DATA_MOCK_GETTER: () => IEntityData[] = () => [
{
id: '1',
value: 1,
Expand All @@ -34,30 +34,32 @@ 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<string> | undefined): Promise<IEntityData[]> {
const data = DATA_MOCK_GETTER();

return new Promise((resolve, reject) => {
setTimeout(() => {
if (key) {
if (key === ERROR_ITEM_ID) {
reject(new Error(TEST_ERROR_MESSAGE));
}

const data = DATA_MOCK.find(d => 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);
});
}

class TestMapResource extends CachedMapResource<string, IEntityData> {
constructor() {
super(DEFAULT_STATE);
super(DEFAULT_STATE_GETTER);
}

protected async loader(key: ResourceKey<string>) {
Expand All @@ -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', () => {
Expand Down

0 comments on commit b696000

Please sign in to comment.