Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / store / states / artifacts.state.spec.ts
1 import { Store } from '@ngxs/store';
2 import { Observable } from 'rxjs/Rx';
3 import { Mock } from 'ts-mockery';
4 import { ArtifactModel } from '../../../models/artifacts';
5 import { ArtifactGroupType } from '../../../utils/constants';
6 import { ComponentInstanceServiceNg2 } from '../../services/component-instance-services/component-instance.service';
7 import { GetInstanceArtifactsByTypeAction, UpdateInstanceArtifactAction } from '../actions/instance-artifacts.actions';
8 import { InstanceArtifactsState } from './instance-artifacts.state';
9
10 describe('Test Artifact State', () => {
11
12     const heat1 = Mock.of<ArtifactModel>({
13         uniqueId: '1', artifactName: 'heat1', timeout: 0, artifactDisplayName: 'heat1', artifactGroupType: ArtifactGroupType.DEPLOYMENT
14     });
15
16     const heat1env = Mock.of<ArtifactModel>({
17         uniqueId: '2', artifactName: 'heat1env', timeout: 0, generatedFromId: '1', artifactDisplayName: 'heat1env', artifactGroupType: ArtifactGroupType.DEPLOYMENT
18     });
19
20     const storeMock = Mock.of<Store>( { dispatch : jest.fn() });
21
22     const artifacts = [
23          heat1,
24          heat1env
25     ];
26
27     /**
28      * NGXS Store state before we run the update
29      */
30     const ngxsState = {
31         deploymentArtifacts : artifacts
32     };
33
34     /**
35      * The ENV artifact that we wish to update
36      */
37     const updatedArtifact = Mock.of<ArtifactModel>({
38         uniqueId: '2', artifactName: 'heat1env', timeout: 33, generatedFromId: '1', artifactDisplayName: 'heat1env-UPDATE', artifactGroupType: ArtifactGroupType.DEPLOYMENT
39     });
40
41     const componentInstanceServiceMock: ComponentInstanceServiceNg2 = Mock.of<ComponentInstanceServiceNg2>({
42         updateInstanceArtifact: jest.fn().mockImplementation(() => Observable.of(updatedArtifact)),
43         getComponentInstanceArtifactsByGroupType: jest.fn().mockImplementation(() => Observable.of([heat1, updatedArtifact]))
44     });
45
46     const actionMock: UpdateInstanceArtifactAction = Mock.of<UpdateInstanceArtifactAction>({
47         payload: {
48             componentType: '',
49             componentId: '',
50             instanceId: '',
51             artifact: updatedArtifact
52         }
53     });
54
55     it('Test that HEAT timeout is updated', () => {
56         const state: InstanceArtifactsState = new InstanceArtifactsState(storeMock, componentInstanceServiceMock);
57         const context = { getState: jest.fn().mockImplementation(() => ngxsState), patchState: jest.fn(), setState: jest.fn(), dispatch: jest.fn() };
58         state.updateArtifact(context, actionMock ).subscribe( (v) => console.log('OK'));
59         expect(storeMock.dispatch).toBeCalled();
60     });
61
62 });