Skip to content

Commit

Permalink
Improve user fetching for metadata view
Browse files Browse the repository at this point in the history
  • Loading branch information
ncovercash committed Oct 29, 2023
1 parent 4148e54 commit b944026
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 300 deletions.
41 changes: 19 additions & 22 deletions src/data/DataRepository.getters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const mutators = {
update: jest.fn(),
delete: jest.fn(),
dates: jest.fn(),
getUser: jest.fn()
};

test('Getters work as expected with undefined objects', () => {
Expand All @@ -23,19 +22,19 @@ test('Get Calendar works with calendar that does not exist, isLoaded with only C
const repository = new DataRepository(
[Calendars.SPRING_SP_1_2, Calendars.SPRING_SP_3_4, Calendars.SUMMER_SP_3],
[],
mutators
mutators,
);
expect(repository.isLoaded()).toBe(true);
expect(
repository.getCalendar('d3f3354c-2986-5d30-a84c-1eflfd613ac6')
repository.getCalendar('d3f3354c-2986-5d30-a84c-1eflfd613ac6'),
).toStrictEqual(undefined);
});

test('Get Calendar works with null and undefined', () => {
const repository = new DataRepository(
[Calendars.SPRING_SP_1_2, Calendars.SPRING_SP_3_4, Calendars.SUMMER_SP_3],
[],
mutators
mutators,
);
expect(repository.isLoaded()).toBe(true);
expect(repository.getCalendar(null)).toStrictEqual(undefined);
Expand All @@ -46,15 +45,13 @@ test('getServicePointFromID works with service point that does not exist, isLoad
const repository = new DataRepository(
[],
[ServicePoints.SERVICE_POINT_1_DTO],
mutators
mutators,
);
expect(repository.isLoaded()).toBe(true);
expect(
repository.getServicePointFromId('a3f3354c-2986-5d33-a84c-1eflfd613ac6')
).toStrictEqual(undefined);
expect(
repository.getServicePointFromId(undefined)
repository.getServicePointFromId('a3f3354c-2986-5d33-a84c-1eflfd613ac6'),
).toStrictEqual(undefined);
expect(repository.getServicePointFromId(undefined)).toStrictEqual(undefined);
});

test('Getters work as expected with empty objects', () => {
Expand All @@ -68,7 +65,7 @@ test('Getters work as expected (1 Calendar, 2 Service Points)', () => {
const repository1 = new DataRepository(
[Calendars.SPRING_SP_1_2],
[ServicePoints.SERVICE_POINT_1_DTO, ServicePoints.SERVICE_POINT_2_DTO],
mutators
mutators,
);

expect(repository1.isLoaded()).toBe(true);
Expand All @@ -77,42 +74,42 @@ test('Getters work as expected (1 Calendar, 2 Service Points)', () => {
expect(repository1.getCalendars()).toStrictEqual([Calendars.SPRING_SP_1_2]);
expect(repository1.getServicePoints()).toStrictEqual([
ServicePoints.SERVICE_POINT_1,
ServicePoints.SERVICE_POINT_2
ServicePoints.SERVICE_POINT_2,
]);

expect(
repository1.getServicePointsFromIds([
'3a40852d-49fd-4df2-a1f9-6e2641a6e91f',
'3b071ddf-14ad-58a1-9fb5-b3737da888de'
])
'3b071ddf-14ad-58a1-9fb5-b3737da888de',
]),
).toStrictEqual([
ServicePoints.SERVICE_POINT_1,
ServicePoints.SERVICE_POINT_2
ServicePoints.SERVICE_POINT_2,
]);

expect(
repository1.getServicePointFromId('3a40852d-49fd-4df2-a1f9-6e2641a6e91f')
repository1.getServicePointFromId('3a40852d-49fd-4df2-a1f9-6e2641a6e91f'),
).toStrictEqual(ServicePoints.SERVICE_POINT_1);
expect(
repository1.getServicePointNamesFromIds([
'3a40852d-49fd-4df2-a1f9-6e2641a6e91f',
'3b071ddf-14ad-58a1-9fb5-b3737da888de'
])
'3b071ddf-14ad-58a1-9fb5-b3737da888de',
]),
).toStrictEqual([
ServicePoints.SERVICE_POINT_1.name,
ServicePoints.SERVICE_POINT_2.name
ServicePoints.SERVICE_POINT_2.name,
]);

expect(
repository1.getCalendar('d3f3354c-2986-5d31-a84c-1ef3fd613ac6')
repository1.getCalendar('d3f3354c-2986-5d31-a84c-1ef3fd613ac6'),
).toStrictEqual(Calendars.SPRING_SP_1_2);
});

test('Getters work as expected (multiple calendars)', () => {
const repository1 = new DataRepository(
[Calendars.SPRING_SP_1_2, Calendars.SPRING_SP_3_4, Calendars.SUMMER_SP_3],
[],
mutators
mutators,
);

expect(repository1.isLoaded()).toBe(true);
Expand All @@ -121,9 +118,9 @@ test('Getters work as expected (multiple calendars)', () => {
expect(repository1.getCalendars()).toStrictEqual([
Calendars.SPRING_SP_1_2,
Calendars.SPRING_SP_3_4,
Calendars.SUMMER_SP_3
Calendars.SUMMER_SP_3,
]);
expect(
repository1.getCalendar('4047ecea-bb24-5f76-9403-d44144c57b66')
repository1.getCalendar('4047ecea-bb24-5f76-9403-d44144c57b66'),
).toStrictEqual(Calendars.SUMMER_SP_3);
});
87 changes: 21 additions & 66 deletions src/data/DataRepository.mutators.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Calendars from '../test/data/Calendars';
import * as Dates from '../test/data/Dates';
import * as Users from '../test/data/Users';
import { Calendar, DailyOpeningInfo, User } from '../types/types';
import { Calendar, DailyOpeningInfo } from '../types/types';
import DataRepository from './DataRepository';

test('Create calendar calls the appropriate mutator', async () => {
Expand All @@ -13,69 +12,67 @@ test('Create calendar calls the appropriate mutator', async () => {
update: jest.fn(),
delete: jest.fn(),
dates: jest.fn(),
getUser: jest.fn()
};

const repository = new DataRepository([], [], mutators);
expect(
await repository.createCalendar(Calendars.ALL_YEAR_SP_ONLINE_247)
await repository.createCalendar(Calendars.ALL_YEAR_SP_ONLINE_247),
).toHaveProperty('id', 'mocked');

expect(mutators.create).toHaveBeenCalledTimes(1);
expect(mutators.create).toHaveBeenLastCalledWith(
Calendars.ALL_YEAR_SP_ONLINE_247
Calendars.ALL_YEAR_SP_ONLINE_247,
);

expect(
await repository.createCalendar(Calendars.ALL_YEAR_SP_ONLINE_247)
await repository.createCalendar(Calendars.ALL_YEAR_SP_ONLINE_247),
).toHaveProperty('name', Calendars.ALL_YEAR_SP_ONLINE_247.name);

expect(mutators.create).toHaveBeenCalledTimes(2);
expect(mutators.create).toHaveBeenLastCalledWith(
Calendars.ALL_YEAR_SP_ONLINE_247
Calendars.ALL_YEAR_SP_ONLINE_247,
);

expect(mutators.update).not.toHaveBeenCalled();
expect(mutators.delete).not.toHaveBeenCalled();
expect(mutators.dates).not.toHaveBeenCalled();
expect(mutators.getUser).not.toHaveBeenCalled();
});

test('Update calendar calls the appropriate mutator', async () => {
const mutators = {
create: jest.fn(),
update: jest.fn(() => Promise.resolve({
...Calendars.ALL_YEAR_SP_ONLINE_247,
id: 'mocked'
})),
update: jest.fn(() =>
Promise.resolve({
...Calendars.ALL_YEAR_SP_ONLINE_247,
id: 'mocked',
}),
),
delete: jest.fn(),
dates: jest.fn(),
getUser: jest.fn()
};

const repository = new DataRepository([], [], mutators);
expect(
await repository.updateCalendar(Calendars.ALL_YEAR_SP_ONLINE_247)
await repository.updateCalendar(Calendars.ALL_YEAR_SP_ONLINE_247),
).toHaveProperty('id', 'mocked');

expect(mutators.update).toHaveBeenCalledTimes(1);
expect(mutators.update).toHaveBeenLastCalledWith(
Calendars.ALL_YEAR_SP_ONLINE_247
Calendars.ALL_YEAR_SP_ONLINE_247,
);

expect(
await repository.updateCalendar(Calendars.ALL_YEAR_SP_ONLINE_247)
await repository.updateCalendar(Calendars.ALL_YEAR_SP_ONLINE_247),
).toHaveProperty('name', Calendars.ALL_YEAR_SP_ONLINE_247.name);

expect(mutators.update).toHaveBeenCalledTimes(2);
expect(mutators.update).toHaveBeenLastCalledWith(
Calendars.ALL_YEAR_SP_ONLINE_247
Calendars.ALL_YEAR_SP_ONLINE_247,
);

expect(mutators.create).not.toHaveBeenCalled();
expect(mutators.delete).not.toHaveBeenCalled();
expect(mutators.dates).not.toHaveBeenCalled();
expect(mutators.getUser).not.toHaveBeenCalled();
});

test('Delete calendar calls the appropriate mutator and encapsulates lists as needed', async () => {
Expand All @@ -84,25 +81,24 @@ test('Delete calendar calls the appropriate mutator and encapsulates lists as ne
update: jest.fn(),
delete: jest.fn<Promise<void>, [Calendar[]]>(() => Promise.resolve()),
dates: jest.fn(),
getUser: jest.fn()
};

const repository = new DataRepository([], [], mutators);

await repository.deleteCalendar(Calendars.ALL_YEAR_SP_ONLINE_247);
expect(mutators.delete).toHaveBeenCalledTimes(1);
expect(mutators.delete).toHaveBeenLastCalledWith([
Calendars.ALL_YEAR_SP_ONLINE_247
Calendars.ALL_YEAR_SP_ONLINE_247,
]);

await repository.deleteCalendars([
Calendars.ALL_YEAR_SP_ONLINE_247,
Calendars.SPRING_SP_1_2
Calendars.SPRING_SP_1_2,
]);
expect(mutators.delete).toHaveBeenCalledTimes(2);
expect(mutators.delete).toHaveBeenLastCalledWith([
Calendars.ALL_YEAR_SP_ONLINE_247,
Calendars.SPRING_SP_1_2
Calendars.SPRING_SP_1_2,
]);
});

Expand All @@ -115,7 +111,6 @@ test('Daily opening information is requested appropriately', async () => {
Promise<DailyOpeningInfo[]>,
[{ servicePointId: string; startDate: string; endDate: string }]
>(() => Promise.resolve([])),
getUser: jest.fn()
};

const repository = new DataRepository([], [], mutators);
Expand All @@ -124,53 +119,13 @@ test('Daily opening information is requested appropriately', async () => {
await repository.getDailyOpeningInfo(
'test-id',
Dates.MAR_1_DATE,
Dates.OCT_1_DATE
)
Dates.OCT_1_DATE,
),
).toStrictEqual([]);
expect(mutators.dates).toHaveBeenCalledTimes(1);
expect(mutators.dates).toHaveBeenLastCalledWith({
servicePointId: 'test-id',
startDate: '2000-03-01',
endDate: '2000-10-01'
});
});

test('Successful user gets work as expected', async () => {
const mutators = {
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
dates: jest.fn(),
getUser: jest.fn<Promise<User>, [{ userId: string; signal?: AbortSignal }]>(
() => Promise.resolve(Users.PETRO_PROKOPOVYCH)
)
};

const repository = new DataRepository([], [], mutators);

expect(await repository.getUser(Users.PETRO_PROKOPOVYCH.id)).toStrictEqual(
Users.PETRO_PROKOPOVYCH
);
expect(mutators.getUser).toHaveBeenCalledTimes(1);
expect(mutators.getUser).toHaveBeenLastCalledWith({
userId: Users.PETRO_PROKOPOVYCH.id,
signal: undefined
endDate: '2000-10-01',
});
});

test('Unsuccessful/aborted user gets work as expected', async () => {
const mutators = {
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
dates: jest.fn(),
getUser: jest.fn<Promise<never>, [{ userId: string }]>(() => Promise.reject())
};

const repository = new DataRepository([], [], mutators);

expect.hasAssertions();
repository
.getUser(Users.PETRO_PROKOPOVYCH.id)
.catch(() => expect(mutators.getUser).toHaveBeenCalledTimes(1));
});
20 changes: 5 additions & 15 deletions src/data/DataRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CalendarDTO,
DailyOpeningInfo,
ServicePoint,
User
} from '../types/types';
import { dateUTCToYYYYMMDD } from '../utils/DateUtils';
import { ServicePointDTO } from './types';
Expand All @@ -16,7 +15,7 @@ const getServicePointMap = memoizee(
map[sp.id] = sp;
});
return map;
}
},
);

interface MutatorsType {
Expand All @@ -28,7 +27,6 @@ interface MutatorsType {
startDate: string;
endDate: string;
}) => Promise<DailyOpeningInfo[]>;
getUser: (params: { userId: string; signal?: AbortSignal }) => Promise<User>;
}

export default class DataRepository {
Expand All @@ -43,7 +41,7 @@ export default class DataRepository {
constructor(
calendars: CalendarDTO[] | undefined,
servicePoints: ServicePointDTO[] | undefined,
mutators: MutatorsType
mutators: MutatorsType,
) {
this.calendarsLoaded = calendars !== undefined;
this.calendars = calendars ?? [];
Expand Down Expand Up @@ -72,7 +70,7 @@ export default class DataRepository {
return this.servicePoints.map((dto) => ({
id: dto.id,
name: dto.name,
inactive: false
inactive: false,
}));
}

Expand Down Expand Up @@ -142,20 +140,12 @@ export default class DataRepository {
getDailyOpeningInfo(
servicePointId: string,
startDate: Date,
endDate: Date
endDate: Date,
): Promise<DailyOpeningInfo[]> {
return this.mutators.dates({
servicePointId,
startDate: dateUTCToYYYYMMDD(startDate),
endDate: dateUTCToYYYYMMDD(endDate)
endDate: dateUTCToYYYYMMDD(endDate),
});
}

/**
* Get a user by ID. Optionally, requests may be aborted when the data is no longer needed
* Errors will NOT be rejected, instead, a never-resolving promise will be returned
*/
getUser(userId: string, signal?: AbortSignal): Promise<User> {
return this.mutators.getUser({ userId, signal });
}
}
Loading

0 comments on commit b944026

Please sign in to comment.