AW-3441 another fix...
All checks were successful
FarmMaps/FarmMapsLib/pipeline/head This commit looks good
All checks were successful
FarmMaps/FarmMapsLib/pipeline/head This commit looks good
This commit is contained in:
87
projects/common/src/fm/services/package.service.spec.ts
Normal file
87
projects/common/src/fm/services/package.service.spec.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import {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';
|
||||
|
||||
describe('PackageService', () => {
|
||||
const initialState = {userPackages: {}} as State;
|
||||
let serviceUnderTest: PackageService;
|
||||
let items;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideMockStore({
|
||||
initialState,
|
||||
selectors: [
|
||||
{
|
||||
selector: appCommonReducer.SelectGetValidUserPackages,
|
||||
value: {
|
||||
'vnd.farmmaps.package.zoning': {
|
||||
id: 'vnd.farmmaps.package.zoning',
|
||||
name: 'zoning package',
|
||||
dataDate: '2022-01-25T00:00:00.000000Z'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}),
|
||||
]
|
||||
});
|
||||
|
||||
const store = TestBed.inject(MockStore);
|
||||
serviceUnderTest = new PackageService(store, null, null);
|
||||
|
||||
items = [
|
||||
{
|
||||
code: '123132',
|
||||
dataDate: '2022-01-25T00:00:00.000000Z',
|
||||
data: {
|
||||
id: 'vnd.farmmaps.package.zoning'
|
||||
}
|
||||
} as IItem,
|
||||
{
|
||||
code: '1231325',
|
||||
dataDate: '2021-01-25T00:00:00.000000Z',
|
||||
data: {
|
||||
id: 'vnd.farmmaps.package.zoning'
|
||||
}
|
||||
} as IItem,
|
||||
{
|
||||
code: '1231325',
|
||||
dataDate: '2022-01-25T00:00:00.000000Z',
|
||||
dataEndDate: '2022-05-25T00:00:00.000000Z',
|
||||
data: {
|
||||
id: 'vnd.farmmaps.package.tipstar'
|
||||
}
|
||||
} as IItem
|
||||
];
|
||||
});
|
||||
|
||||
it('Does not have a tipstar package', () => {
|
||||
const hasPackage = serviceUnderTest.hasPackage('vnd.farmmaps.package.tipstar');
|
||||
expect(hasPackage).toBe(false);
|
||||
});
|
||||
|
||||
it('Does have a zoning package', () => {
|
||||
const hasPackage = serviceUnderTest.hasPackage('vnd.farmmaps.package.zoning');
|
||||
expect(hasPackage).toBe(true);
|
||||
});
|
||||
|
||||
it('reducer.INITUSERPACKAGESSUCCESS alter state correctly', () => {
|
||||
const action = new appCommonActions.InitUserPackagesSuccess(items);
|
||||
|
||||
expect(Object.keys(initialState.userPackages).length).toBe(0);
|
||||
const state = reducer(initialState, action);
|
||||
expect(Object.keys(state.userPackages).length).toBe(2);
|
||||
expect(state.userPackages['vnd.farmmaps.package.zoning'].length).toBe(2);
|
||||
expect(state.userPackages['vnd.farmmaps.package.tipstar'].length).toBe(1);
|
||||
|
||||
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'][1])).toBe(true);
|
||||
});
|
||||
});
|
@@ -1,7 +1,7 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Store} from '@ngrx/store';
|
||||
import * as appCommonReducer from '../reducers/app-common.reducer';
|
||||
import {IPackage} from '../models/package';
|
||||
import {IPackage, IPackages} from '../models/package';
|
||||
|
||||
import {IItem} from '../models/item';
|
||||
import {IItemTask} from '../models/itemTask';
|
||||
@@ -35,6 +35,22 @@ export class PackageService {
|
||||
}
|
||||
}
|
||||
|
||||
export function getValidPackages(packageMap: IPackages): {[key: string]: IPackage} {
|
||||
const keys = Object.keys(packageMap);
|
||||
return keys.filter(k => {
|
||||
const packages = packageMap[k]
|
||||
.filter((p) => isValidPackage(p));
|
||||
|
||||
return packages.length > 0;
|
||||
}).reduce((map, key) => {
|
||||
const packages = packageMap[key];
|
||||
return {
|
||||
...map,
|
||||
key: packages[0]
|
||||
};
|
||||
}, {});
|
||||
}
|
||||
|
||||
export function isValidPackage(pack: IPackage): boolean {
|
||||
const today = new Date(new Date(Date.now()).toUTCString()).setHours(0, 0, 0, 0);
|
||||
return pack !== null && new Date(pack.dataDate).getTime() <= today
|
||||
|
Reference in New Issue
Block a user