Skip to content

Commit

Permalink
feat(geography): create subdivisions in country factory (#2611)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Nov 21, 2023
1 parent 8a2f596 commit 92a734b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe('Driver | Testing | Geography | GeographyService', () => {
countryFactoryService = TestBed.inject(DaffCountryFactory);
subdivisionFactoryService = TestBed.inject(DaffSubdivisionFactory);

countryFactory = new DaffCountryFactory();
subdivisionFactory = new DaffSubdivisionFactory();
countryFactory = new DaffCountryFactory(subdivisionFactory);

mockCountry = countryFactory.create();
mockSubdivision = subdivisionFactory.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TestBed } from '@angular/core/testing';

import {
DaffCountry,
DaffSubdivision,
Expand All @@ -6,6 +8,7 @@ import {
DaffCountryLoadSuccess,
DaffCountryListSuccess,
daffCountryEntitiesInitialState as initialState,
DaffCountryEntityState,
} from '@daffodil/geography/state';
import {
DaffCountryFactory,
Expand All @@ -14,15 +17,15 @@ import {

import { daffCountryEntitiesReducer as reducer } from './country-entities.reducer';

describe('Geography | Reducer | CountryEntities', () => {
describe('@daffodil/geography/state | daffCountryEntitiesReducer', () => {
let countryFactory: DaffCountryFactory;
let subdivisionFactory: DaffSubdivisionFactory;
let mockCountry: DaffCountry;
let mockSubdivision: DaffSubdivision;
let countryId: DaffCountry['id'];

beforeEach(() => {
countryFactory = new DaffCountryFactory();
countryFactory = TestBed.inject(DaffCountryFactory);
subdivisionFactory = new DaffSubdivisionFactory();

mockCountry = countryFactory.create();
Expand All @@ -41,7 +44,7 @@ describe('Geography | Reducer | CountryEntities', () => {
});

describe('when CountryLoadSuccessAction is triggered', () => {
let result;
let result: DaffCountryEntityState;

beforeEach(() => {
const countryLoadSuccess = new DaffCountryLoadSuccess<DaffCountry>(mockCountry);
Expand All @@ -59,7 +62,7 @@ describe('Geography | Reducer | CountryEntities', () => {
});

describe('when CountryListSuccessAction is triggered', () => {
let result;
let result: DaffCountryEntityState;

beforeEach(() => {
const countryListSuccess = new DaffCountryListSuccess([mockCountry]);
Expand Down Expand Up @@ -90,15 +93,18 @@ describe('Geography | Reducer | CountryEntities', () => {
...mockCountry,
subdivisions: [mockSubdivision],
});
const countryListSuccess = new DaffCountryListSuccess([mockCountry]);
const countryListSuccess = new DaffCountryListSuccess([{
...mockCountry,
subdivisions: [],
}]);

const inter = reducer(initialState, countryLoadSuccess);

result = reducer(inter, countryListSuccess);
});

it('should not overwrite the subdivisions', () => {
expect(result.entities[mockCountry.id].subdivisions).toEqual([mockSubdivision]);
expect(result.entities[mockCountry.id].subdivisions).toContain(mockSubdivision);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TestBed } from '@angular/core/testing';

import { DaffStateError } from '@daffodil/core/state';
import { DaffCountry } from '@daffodil/geography';
import {
Expand All @@ -14,13 +16,13 @@ import { DaffCountryFactory } from '@daffodil/geography/testing';

import { daffGeographyReducer as reducer } from './geography.reducer';

describe('Geography | Reducer | Geography', () => {
describe('@daffodil/geography/state | daffGeographyReducer', () => {
let countryFactory: DaffCountryFactory;
let country: DaffCountry;
let countryId: DaffCountry['id'];

beforeEach(() => {
countryFactory = new DaffCountryFactory();
countryFactory = TestBed.inject(DaffCountryFactory);

country = countryFactory.create();
countryId = country.id;
Expand Down
14 changes: 11 additions & 3 deletions libs/geography/testing/src/factories/country.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import { faker } from '@faker-js/faker/locale/en_US';
import { DaffModelFactory } from '@daffodil/core/testing';
import { DaffCountry } from '@daffodil/geography';

import { DaffSubdivisionFactory } from './subdivision.factory';

export class MockCountry implements DaffCountry {
id = faker.datatype.uuid();
name = faker.random.word();
name_en = faker.random.word();
alpha2 = faker.random.alphaNumeric(2);
alpha3 = faker.random.alphaNumeric(3);
subdivisions = [];
subdivisions = this.subdivisionFactory.createMany(faker.datatype.number({ min: 0, max: 10 }));

constructor(
protected subdivisionFactory: DaffSubdivisionFactory,
) {}
}

@Injectable({
providedIn: 'root',
})
export class DaffCountryFactory extends DaffModelFactory<DaffCountry> {
constructor() {
super(MockCountry);
constructor(
subdivisionFactory: DaffSubdivisionFactory,
) {
super(MockCountry, subdivisionFactory);
}
}

0 comments on commit 92a734b

Please sign in to comment.