AW-3441 better test to find real problem. fixed the getValidPackages function.
All checks were successful
FarmMaps/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Mark van der Wal 2022-01-27 23:20:20 +01:00
parent 122563a0bd
commit a1a4fc14ab
4 changed files with 47 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "farmmaps-lib-app", "name": "farmmaps-lib-app",
"version": "1.1.10", "version": "1.1.11",
"scripts": { "scripts": {
"ng": "ng", "ng": "ng",
"start": "ng serve", "start": "ng serve",

View File

@ -35,7 +35,7 @@ export class HasPackageDirective implements OnDestroy, AfterViewInit {
constructor(private hostTemplateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef, constructor(private hostTemplateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef,
private store: Store<appCommonReducer.State>, private packageService: PackageService) { private store: Store<appCommonReducer.State>, private packageService: PackageService) {
this.thenTemplate = hostTemplateRef; this.thenTemplate = hostTemplateRef;
this.packSub = this.store.select(appCommonReducer.SelectGetUserPackages).subscribe((_) => { this.packSub = this.store.select(appCommonReducer.SelectGetValidUserPackages).subscribe((_) => {
this.updateView(); this.updateView();
}); });
} }

View File

@ -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 {MockStore, provideMockStore} from '@ngrx/store/testing';
import {TestBed} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing';
import * as appCommonReducer from '../reducers/app-common.reducer'; import * as appCommonReducer from '../reducers/app-common.reducer';
import {reducer, State} from '../reducers/app-common.reducer'; import {reducer, State} from '../reducers/app-common.reducer';
import * as appCommonActions from '../actions/app-common.actions'; import * as appCommonActions from '../actions/app-common.actions';
import {IItem} from '../models/item'; import {IItem} from '../models/item';
import {IPackage} from '../models/package';
describe('PackageService', () => { describe('PackageService', () => {
const initialState = {userPackages: {}} as State; const initialState = {userPackages: {}} as State;
@ -38,25 +39,35 @@ describe('PackageService', () => {
items = [ items = [
{ {
code: '123132', code: '123132',
dataDate: '2022-01-25T00:00:00.000000Z', dataDate: '2021-01-25T00:00:00Z',
dataEndDate: '2021-05-25T00:00:00Z',
data: { data: {
id: 'vnd.farmmaps.package.zoning' id: 'vnd.farmmaps.package.zoning'
} }
} as IItem, } as IItem,
{ {
code: '1231325', code: '1231325',
dataDate: '2021-01-25T00:00:00.000000Z', dataDate: '2021-01-25T00:00:00Z',
dataEndDate: null,
data: { data: {
id: 'vnd.farmmaps.package.zoning' id: 'vnd.farmmaps.package.zoning'
} }
} as IItem, } as IItem,
{ {
code: '1231325', code: '1231325',
dataDate: '2022-01-25T00:00:00.000000Z', dataDate: '2022-01-25T00:00:00Z',
dataEndDate: '2022-05-25T00:00:00.000000Z', dataEndDate: '2022-05-25T00:00:00Z',
data: { data: {
id: 'vnd.farmmaps.package.tipstar' 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 } as IItem
]; ];
}); });
@ -76,12 +87,36 @@ describe('PackageService', () => {
expect(Object.keys(initialState.userPackages).length).toBe(0); expect(Object.keys(initialState.userPackages).length).toBe(0);
const state = reducer(initialState, action); 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.zoning'].length).toBe(2);
expect(state.userPackages['vnd.farmmaps.package.tipstar'].length).toBe(1); 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.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.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);
}); });
}); });

View File

@ -44,10 +44,9 @@ export function getValidPackages(packageMap: IPackages): {[key: string]: IPackag
return packages.length > 0; return packages.length > 0;
}).reduce((map, key) => { }).reduce((map, key) => {
const packages = packageMap[key]; const packages = packageMap[key];
return { const newMap = {...map};
...map, newMap[key] = packages.find(p => isValidPackage(p));
key: packages[0] return newMap;
};
}, {}); }, {});
} }