From a1a4fc14ab847601dcebc20906c4584bf354af16 Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Thu, 27 Jan 2022 23:20:20 +0100 Subject: [PATCH] AW-3441 better test to find real problem. fixed the getValidPackages function. --- package.json | 2 +- .../has-package/has-package.directive.ts | 2 +- .../src/fm/services/package.service.spec.ts | 49 ++++++++++++++++--- .../common/src/fm/services/package.service.ts | 7 ++- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 05f5bbe..e18b414 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "farmmaps-lib-app", - "version": "1.1.10", + "version": "1.1.11", "scripts": { "ng": "ng", "start": "ng serve", diff --git a/projects/common/src/fm/components/has-package/has-package.directive.ts b/projects/common/src/fm/components/has-package/has-package.directive.ts index 02b5823..32f7efd 100644 --- a/projects/common/src/fm/components/has-package/has-package.directive.ts +++ b/projects/common/src/fm/components/has-package/has-package.directive.ts @@ -35,7 +35,7 @@ export class HasPackageDirective implements OnDestroy, AfterViewInit { constructor(private hostTemplateRef: TemplateRef, private viewContainerRef: ViewContainerRef, private store: Store, private packageService: PackageService) { this.thenTemplate = hostTemplateRef; - this.packSub = this.store.select(appCommonReducer.SelectGetUserPackages).subscribe((_) => { + this.packSub = this.store.select(appCommonReducer.SelectGetValidUserPackages).subscribe((_) => { this.updateView(); }); } diff --git a/projects/common/src/fm/services/package.service.spec.ts b/projects/common/src/fm/services/package.service.spec.ts index 1838e0f..09a1252 100644 --- a/projects/common/src/fm/services/package.service.spec.ts +++ b/projects/common/src/fm/services/package.service.spec.ts @@ -1,10 +1,11 @@ -import {isValidPackage, PackageService} from './package.service'; +import {getValidPackages, isValidPackage, PackageService} from './package.service'; import {MockStore, provideMockStore} from '@ngrx/store/testing'; import {TestBed} from '@angular/core/testing'; import * as appCommonReducer from '../reducers/app-common.reducer'; import {reducer, State} from '../reducers/app-common.reducer'; import * as appCommonActions from '../actions/app-common.actions'; import {IItem} from '../models/item'; +import {IPackage} from '../models/package'; describe('PackageService', () => { const initialState = {userPackages: {}} as State; @@ -38,25 +39,35 @@ describe('PackageService', () => { items = [ { code: '123132', - dataDate: '2022-01-25T00:00:00.000000Z', + dataDate: '2021-01-25T00:00:00Z', + dataEndDate: '2021-05-25T00:00:00Z', data: { id: 'vnd.farmmaps.package.zoning' } } as IItem, { code: '1231325', - dataDate: '2021-01-25T00:00:00.000000Z', + dataDate: '2021-01-25T00:00:00Z', + dataEndDate: null, data: { id: 'vnd.farmmaps.package.zoning' } } as IItem, { code: '1231325', - dataDate: '2022-01-25T00:00:00.000000Z', - dataEndDate: '2022-05-25T00:00:00.000000Z', + dataDate: '2022-01-25T00:00:00Z', + dataEndDate: '2022-05-25T00:00:00Z', data: { id: 'vnd.farmmaps.package.tipstar' } + } as IItem, + { + code: '1231325', + dataDate: '2022-01-25T00:00:00Z', + dataEndDate: null, + data: { + id: 'vnd.farmmaps.package.weather' + } } as IItem ]; }); @@ -76,12 +87,36 @@ describe('PackageService', () => { expect(Object.keys(initialState.userPackages).length).toBe(0); const state = reducer(initialState, action); - expect(Object.keys(state.userPackages).length).toBe(2); + expect(Object.keys(state.userPackages).length).toBe(3); expect(state.userPackages['vnd.farmmaps.package.zoning'].length).toBe(2); expect(state.userPackages['vnd.farmmaps.package.tipstar'].length).toBe(1); + expect(state.userPackages['vnd.farmmaps.package.weather'].length).toBe(1); + }); + it('to validatePackage', () => { + const action = new appCommonActions.InitUserPackagesSuccess(items); + const state = reducer(initialState, action); + + expect(isValidPackage(null)).toBe(false); expect(isValidPackage(state.userPackages['vnd.farmmaps.package.tipstar'][0])).toBe(true); - expect(isValidPackage(state.userPackages['vnd.farmmaps.package.zoning'][0])).toBe(true); + expect(isValidPackage(state.userPackages['vnd.farmmaps.package.zoning'][0])).toBe(false); expect(isValidPackage(state.userPackages['vnd.farmmaps.package.zoning'][1])).toBe(true); + expect(isValidPackage(state.userPackages['vnd.farmmaps.package.weather'][0])).toBe(true); + }); + + it('to getValidPackages', () => { + const action = new appCommonActions.InitUserPackagesSuccess(items); + const state = reducer(initialState, action); + + const validPackages = getValidPackages(state.userPackages); + + expect(Object.keys(validPackages).length).toBe(3); + + // today is 27-01-2022, so the below package is not valid + expect(validPackages['vnd.farmmaps.package.zoning']).not.toEqual({ + id: 'vnd.farmmaps.package.zoning', + dataDate: '2021-01-25T00:00:00Z', + dataEndDate: '2021-05-25T00:00:00Z' + } as unknown as IPackage); }); }); diff --git a/projects/common/src/fm/services/package.service.ts b/projects/common/src/fm/services/package.service.ts index 43fe85d..588e49e 100644 --- a/projects/common/src/fm/services/package.service.ts +++ b/projects/common/src/fm/services/package.service.ts @@ -44,10 +44,9 @@ export function getValidPackages(packageMap: IPackages): {[key: string]: IPackag return packages.length > 0; }).reduce((map, key) => { const packages = packageMap[key]; - return { - ...map, - key: packages[0] - }; + const newMap = {...map}; + newMap[key] = packages.find(p => isValidPackage(p)); + return newMap; }, {}); }