Skip to content

Commit

Permalink
EntityCollection: rename entities->models
Browse files Browse the repository at this point in the history
  • Loading branch information
harunou committed Mar 27, 2024
1 parent 65087b4 commit 27b6415
Show file tree
Hide file tree
Showing 25 changed files with 149 additions and 172 deletions.
4 changes: 2 additions & 2 deletions src/@types/Entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Entity<TDto> {
dto: TDto;
setData(data: TDto): void;
patchData(data: Partial<TDto>): void;
setData(dto: TDto): void;
patchData(dto: Partial<TDto>): void;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- NOTE(harunou): any is needed here to infer primitive types
Expand Down
8 changes: 4 additions & 4 deletions src/@types/EntityCollection.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { UniqueEntityDto, CollectableEntity } from './Entity';

// NOTE(harunou): look into RTK entity collection interface
export interface EntityCollection<
TDto extends UniqueEntityDto,
TEntity extends CollectableEntity<TDto>,
> {
readonly entities: TEntity[];
readonly models: TEntity[];
readonly ids: Array<TEntity['id']>;
readonly amountOfEntities: number;
readonly hasEntities: boolean;
readonly length: number;

get(id: TEntity['id'] | undefined): TEntity | undefined;

hasEntity(id: TEntity['id'] | undefined): boolean;
has(id: TEntity['id'] | undefined): boolean;

add(entity: TEntity): void;

Expand Down
2 changes: 1 addition & 1 deletion src/modules/orders/models/OrderModel/OrderModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class OrderModel implements OrderEntity {
return {
id: this.id,
userId: this.userId,
items: this.items.entities.map((item) => item.dto),
items: this.items.models.map((item) => item.dto),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class TotalOrderItemQuantitySelectorNotOptimized implements Selector<[],

select(): number {
this.calculations.select += 1;
return this.orderModelCollection.entities
.flatMap((order) => order.items.entities)
return this.orderModelCollection.models
.flatMap((order) => order.items.models)
.reduce((total, item) => total + item.quantity, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class TotalOrderItemQuantitySelectorOptimized implements Selector<[], num
@computed
get totalQuantity(): number {
this.calculations.totalQuantity += 1;
return this.orderModelCollection.entities
.flatMap((order) => order.items.entities)
return this.orderModelCollection.models
.flatMap((order) => order.items.models)
.reduce((total, item) => total + item.quantity, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class TotalOrderItemQuantitySelectorSingleton implements Selector<[], num

@computed
get totalQuantity(): number {
return this.orderModelCollection.entities
.flatMap((order) => order.items.entities)
return this.orderModelCollection.models
.flatMap((order) => order.items.models)
.reduce((total, item) => total + item.quantity, 0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/orders/stores/OrdersStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class OrdersStore implements OrdersAggregate {

get dto(): OrdersAggregateDto {
return {
orderModelCollectionDto: this.orderModelCollection.entities.map((entity) => entity.dto),
orderModelCollectionDto: this.orderModelCollection.models.map((entity) => entity.dto),
ordersPresentationModelDto: this.ordersPresentationModel.dto,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe.each([

await tick();

const resultOrderIds = store.orderModelCollection.entities.map((order) => order.id);
const resultOrderIds = store.orderModelCollection.models.map((order) => order.id);
expect(resultOrderIds).not.toContain(order.id);
expect(resultOrderIds.length).toBe(amountOfOrders - 1);
});
Expand All @@ -59,7 +59,7 @@ describe.each([

await tick();

const resultOrderIds = store.orderModelCollection.entities.map((order) => order.id);
const resultOrderIds = store.orderModelCollection.models.map((order) => order.id);
expect(resultOrderIds).toContain(order.id);
expect(resultOrderIds.length).toBe(amountOfOrders);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe(`${DeleteOrderUseCase.name}`, () => {

await tick();

const resultOrderIds = store.orderModelCollection.entities.map((order) => order.id);
const resultOrderIds = store.orderModelCollection.models.map((order) => order.id);
expect(resultOrderIds).not.toContain(order.id);
expect(resultOrderIds.length).toBe(amountOfOrders - 1);
});
Expand All @@ -55,7 +55,7 @@ describe(`${DeleteOrderUseCase.name}`, () => {

await tick();

const resultOrderIds = store.orderModelCollection.entities.map((order) => order.id);
const resultOrderIds = store.orderModelCollection.models.map((order) => order.id);
expect(resultOrderIds).toContain(order.id);
expect(resultOrderIds.length).toBe(amountOfOrders);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
ServiceGateway,
} from '../../types';

export type OrderEntityCollectionDep = Pick<OrderEntityCollection, 'remove' | 'entities'>;
export type OrderEntityCollectionDep = Pick<OrderEntityCollection, 'remove' | 'models'>;
export type OrdersPresentationEntityDep = Pick<OrdersPresentationEntity, 'patchData'>;
export type ServiceGatewayDep = Pick<ServiceGateway, 'logOrders'>;
export type DeleteOrderEffectDep = Effect<[string]>;
Expand Down Expand Up @@ -40,7 +40,7 @@ export class DeleteOrderUseCase implements UseCase<[string]> {
await this.deleteOrderEffect.run(id);

this.successTransaction(id);
void this.serviceGateway.logOrders(this.orderModelCollection.entities);
void this.serviceGateway.logOrders(this.orderModelCollection.models);
} catch (error: unknown) {
this.failureTransaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe(`${DeleteOrderUseCase.name}`, () => {

beforeEach(() => {
orderModelCollection = {
entities: [],
models: [],
remove: jest.fn(),
};
ordersPresentationModel = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
OrdersGateway,
} from '../../types';

export type OrderEntityCollectionDep = Pick<OrderEntityCollection, 'remove' | 'entities'>;
export type OrderEntityCollectionDep = Pick<OrderEntityCollection, 'remove' | 'models'>;
export type OrdersPresentationEntityDep = Pick<OrdersPresentationEntity, 'patchData'>;
export type ServiceGatewayDep = Pick<ServiceGateway, 'logOrders'>;
export type OrdersGatewayDep = Pick<OrdersGateway, 'deleteOrder'>;
Expand Down Expand Up @@ -36,7 +36,7 @@ export class DeleteOrderUseCase implements UseCase<[string]> {
await this.deleteOrderEffect(id);

this.successTransaction(id);
void this.serviceGateway.logOrders(this.orderModelCollection.entities);
void this.serviceGateway.logOrders(this.orderModelCollection.models);
} catch (error: unknown) {
this.failureTransaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class OrderPresenter {

@computed
get itemIds(): string[] {
return this.order?.items.entities.map((item) => item.id) ?? [];
return this.order?.items.models.map((item) => item.id) ?? [];
}

@computed
Expand Down
16 changes: 8 additions & 8 deletions src/modules/orders/views/containers/OrderItem/OrderItem.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe.each([
ordersStore.orderModelCollection.replaceAllFromDto(orderModelCollectionDto);
});
it('renders item data if supplied ids refer to a existing orderItem', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const item0 = order0.items.entities[orderItemsAmount - 1];
const item0 = order0.items.models[orderItemsAmount - 1];
assert(item0);

render(
Expand All @@ -57,13 +57,13 @@ describe.each([
expect(quantityElement).toBeInTheDocument();
});
it('renders correct item data if props are changed', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const order1 = ordersStore.orderModelCollection.entities[ordersAmount - 2];
const order1 = ordersStore.orderModelCollection.models[ordersAmount - 2];
assert(order1);
const item00 = order0.items.entities[orderItemsAmount - 1];
const item00 = order0.items.models[orderItemsAmount - 1];
assert(item00);
const item01 = order0.items.entities[orderItemsAmount - 2];
const item01 = order0.items.models[orderItemsAmount - 2];
assert(item01);

const { rerender } = render(
Expand All @@ -87,9 +87,9 @@ describe.each([
expect(quantityElement).toBeInTheDocument();
});
it('renders updated item data when item quantity is updated', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const item0 = order0.items.entities[orderItemsAmount - 1];
const item0 = order0.items.models[orderItemsAmount - 1];
assert(item0);

render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe(`${OrderItem.name}`, () => {
ordersStore.orderModelCollection.replaceAllFromDto(orderModelCollectionDto);
});
it('has expected amount of renders in OrderItem', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const order1 = ordersStore.orderModelCollection.entities[ordersAmount - 2];
const order1 = ordersStore.orderModelCollection.models[ordersAmount - 2];
assert(order1);
const item00 = order0.items.entities[orderItemsAmount - 1];
const item00 = order0.items.models[orderItemsAmount - 1];
assert(item00);
const item01 = order0.items.entities[orderItemsAmount - 2];
const item01 = order0.items.models[orderItemsAmount - 2];
assert(item01);
const item11 = order1.items.entities[orderItemsAmount - 1];
const item11 = order1.items.models[orderItemsAmount - 1];
assert(item11);
const rendersCounter = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe(`${OrderItem.name}`, () => {
ordersStore.orderModelCollection.replaceAllFromDto(orderModelCollectionDto);
});
it('has expected amount of renders in OrderItem', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const order1 = ordersStore.orderModelCollection.entities[ordersAmount - 2];
const order1 = ordersStore.orderModelCollection.models[ordersAmount - 2];
assert(order1);
const item00 = order0.items.entities[orderItemsAmount - 1];
const item00 = order0.items.models[orderItemsAmount - 1];
assert(item00);
const item01 = order0.items.entities[orderItemsAmount - 2];
const item01 = order0.items.models[orderItemsAmount - 2];
assert(item01);
const item11 = order1.items.entities[orderItemsAmount - 1];
const item11 = order1.items.models[orderItemsAmount - 1];
assert(item11);
const rendersCounter = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe(`${OrderItem.name}`, () => {
ordersStore.orderModelCollection.replaceAllFromDto(orderModelCollectionDto);
});
it('has expected amount of renders in OrderItem', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const order1 = ordersStore.orderModelCollection.entities[ordersAmount - 2];
const order1 = ordersStore.orderModelCollection.models[ordersAmount - 2];
assert(order1);
const item00 = order0.items.entities[orderItemsAmount - 1];
const item00 = order0.items.models[orderItemsAmount - 1];
assert(item00);
const item01 = order0.items.entities[orderItemsAmount - 2];
const item01 = order0.items.models[orderItemsAmount - 2];
assert(item01);
const item11 = order1.items.entities[orderItemsAmount - 1];
const item11 = order1.items.models[orderItemsAmount - 1];
assert(item11);
const rendersCounter = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe(`${OrderItem.name}`, () => {
ordersStore.orderModelCollection.replaceAllFromDto(orderModelCollectionDto);
});
it('has expected amount of renders in OrderItem', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const order1 = ordersStore.orderModelCollection.entities[ordersAmount - 2];
const order1 = ordersStore.orderModelCollection.models[ordersAmount - 2];
assert(order1);
const item00 = order0.items.entities[orderItemsAmount - 1];
const item00 = order0.items.models[orderItemsAmount - 1];
assert(item00);
const item01 = order0.items.entities[orderItemsAmount - 2];
const item01 = order0.items.models[orderItemsAmount - 2];
assert(item01);
const item11 = order1.items.entities[orderItemsAmount - 1];
const item11 = order1.items.models[orderItemsAmount - 1];
assert(item11);
const rendersCounter = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe(`${OrderItem.name}`, () => {
ordersStore.orderModelCollection.replaceAllFromDto(orderModelCollectionDto);
});
it('has expected amount of renders in OrderItem', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const order1 = ordersStore.orderModelCollection.entities[ordersAmount - 2];
const order1 = ordersStore.orderModelCollection.models[ordersAmount - 2];
assert(order1);
const item00 = order0.items.entities[orderItemsAmount - 1];
const item00 = order0.items.models[orderItemsAmount - 1];
assert(item00);
const item01 = order0.items.entities[orderItemsAmount - 2];
const item01 = order0.items.models[orderItemsAmount - 2];
assert(item01);
const item11 = order1.items.entities[orderItemsAmount - 1];
const item11 = order1.items.models[orderItemsAmount - 1];
assert(item11);
const rendersCounter = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ describe(`${OrderItem.name}`, () => {
ordersStore.orderModelCollection.replaceAllFromDto(orderModelCollectionDto);
});
it('has expected amount of renders in OrderItem', () => {
const order0 = ordersStore.orderModelCollection.entities[ordersAmount - 1];
const order0 = ordersStore.orderModelCollection.models[ordersAmount - 1];
assert(order0);
const order1 = ordersStore.orderModelCollection.entities[ordersAmount - 2];
const order1 = ordersStore.orderModelCollection.models[ordersAmount - 2];
assert(order1);
const item00 = order0.items.entities[orderItemsAmount - 1];
const item00 = order0.items.models[orderItemsAmount - 1];
assert(item00);
const item01 = order0.items.entities[orderItemsAmount - 2];
const item01 = order0.items.models[orderItemsAmount - 2];
assert(item01);
const item11 = order1.items.entities[orderItemsAmount - 1];
const item11 = order1.items.models[orderItemsAmount - 1];
assert(item11);
const rendersCounter = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,41 +72,41 @@ describe(`${OrderItemController.name}`, () => {
jest.clearAllMocks();
});
it('remove an item from an existing order', () => {
const orderModel = orderModelCollection.entities.at(3);
const orderModel = orderModelCollection.models.at(3);
assert(orderModel);
const itemEntity = orderModel.items.entities.at(2);
const itemEntity = orderModel.items.models.at(2);
assert(itemEntity);

orderIdStore = { value: orderModel.id };
itemIdStore = { value: itemEntity.id };
controller = new OrderItemController(orderIdStore, itemIdStore, orderByIdSelector);

const expectedOrdersAmount = orderModel.items.entities.length - 1;
const expectedOrdersAmount = orderModel.items.models.length - 1;

controller.deleteButtonClicked();

const resultOrdersAmount = orderModel.items.entities.length;
const resultModel = orderModel.items.entities.find(
const resultOrdersAmount = orderModel.items.models.length;
const resultModel = orderModel.items.models.find(
(entity) => entity.id === itemEntity.id,
);

expect(resultOrdersAmount).toBe(expectedOrdersAmount);
expect(resultModel).toBeUndefined();
});
it('does not remove an item from a non-existing order', () => {
const orderModel = orderModelCollection.entities.at(3);
const orderModel = orderModelCollection.models.at(3);
assert(orderModel);

orderIdStore = { value: orderModel.id };
itemIdStore = { value: '9000' };

controller = new OrderItemController(orderIdStore, itemIdStore, orderByIdSelector);

const expectedOrdersAmount = orderModel.items.entities.length;
const expectedOrdersAmount = orderModel.items.models.length;

controller.deleteButtonClicked();

const resultOrdersAmount = orderModel.items.entities.length;
const resultOrdersAmount = orderModel.items.models.length;

expect(resultOrdersAmount).toBe(expectedOrdersAmount);
});
Expand Down
Loading

0 comments on commit 27b6415

Please sign in to comment.