blob: 61e93cb9d22efb1d1c0eddcc206e62b1ba1ada67 [file] [log] [blame]
svishnev1eb66b72018-01-11 14:39:45 +02001/*
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02002 * Copyright © 2016-2018 European Support Limited
talig8e9c0652017-12-20 14:30:43 +02003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02007 *
svishnev1eb66b72018-01-11 14:39:45 +02008 * http://www.apache.org/licenses/LICENSE-2.0
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02009 *
talig8e9c0652017-12-20 14:30:43 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
svishnev1eb66b72018-01-11 14:39:45 +020012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
talig8e9c0652017-12-20 14:30:43 +020015 */
16
17import React from 'react';
Einav Weiss Keidard2f57942018-02-14 14:00:07 +020018import ShallowRenderer from 'react-test-renderer/shallow';
talig8e9c0652017-12-20 14:30:43 +020019import deepFreeze from 'deep-freeze';
20import mockRest from 'test-utils/MockRest.js';
21import Configuration from 'sdc-app/config/Configuration.js';
22import {storeCreator} from 'sdc-app/AppStore.js';
23
24import {mapStateToProps, mapActionsToProps} from 'sdc-app/onboarding/versionsPage/VersionsPage.js';
25import VersionsPageView from 'sdc-app/onboarding/versionsPage/VersionsPage.jsx';
26import VersionsPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
27import VersionsPageCreationActionHelper from 'sdc-app/onboarding/versionsPage/creation/VersionsPageCreationActionHelper.js';
28import {itemTypes as versionPageItemTypes} from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
29import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
30import {VersionsPageCreationFactory} from 'test-utils/factories/versionsPage/VersionsPageCreationFactories.js';
31import {VersionsPageAdditionalPropsFactory} from 'test-utils/factories/versionsPage/VersionsPageFactories.js';
32
33describe('Versions Page Module Tests', () => {
34
35 const state = {
36 currentScreen: {
37 itemPermission: {
38 isCollaborator: true
39 },
40 props: {
41 itemId: '23'
42 }
43 },
44 users: {
45 userInfo: 'user123'
46 },
47 versionsPage: {
48 permissions: {},
49 versionsList: {versions: []},
50 versionCreation: {}
51 }
52 };
53
54 it ('mapStateToProps mapper exists', () => {
55
56 expect(mapStateToProps).toBeTruthy();
57
58 });
59
60 it ('should have state in props', () => {
61
62 const props = mapStateToProps(state);
63 expect(props.currentUser).toEqual('user123');
64
65 });
66
67 it('simple jsx test', () => {
68
69 const store = storeCreator();
70 const dispatch = store.dispatch;
71
72 const additionalProps = VersionsPageAdditionalPropsFactory.build();
73
74 const props = Object.assign({}, mapStateToProps(state), mapActionsToProps(dispatch, additionalProps));
Einav Weiss Keidard2f57942018-02-14 14:00:07 +020075 const renderer = new ShallowRenderer();
talig8e9c0652017-12-20 14:30:43 +020076 renderer.render(<VersionsPageView {...props} />);
77
78 const renderedOutput = renderer.getRenderOutput();
79 expect(renderedOutput).toBeTruthy();
80
81 });
82
83});
84
85describe('Versions Page Actions', () => {
86
87 let store, dispatch, restPrefix = '', version;
88
89 beforeAll(() => {
90 restPrefix = Configuration.get('restPrefix');
91 store = storeCreator();
92 dispatch = store.dispatch;
93 deepFreeze(store.getState());
94 version = VersionFactory.build();
95 });
96
97 it('Select and deselect version', () => {
98
99 let selectedVersionOf = (state) => state.versionsPage.versionsList.selectedVersion;
100 VersionsPageActionHelper.selectVersion(dispatch, {version});
101 expect(selectedVersionOf(store.getState())).toEqual(version.id);
102 VersionsPageActionHelper.selectNone(dispatch);
103 expect(selectedVersionOf(store.getState())).toEqual(null);
104
105 });
106
107 it('Create version', () => {
108
109 const {id, baseId} = version;
110
111 mockRest.addHandler('post', ({baseUrl}) => {
112 expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions/${baseId}/`);
113 return version;
114 });
115
116 mockRest.addHandler('fetch', ({baseUrl}) => {
117 expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions`);
118 return {results: [version]};
119 });
120
121 return VersionsPageCreationActionHelper.createVersion(dispatch, {
122 itemId: id,
123 baseVersion: {id: baseId},
124 payload: VersionsPageCreationFactory.build()
125 })
126 .then(result => {
127 expect(result.id).toEqual(id);
128 });
129
130 });
131
132 it('Fetch versions', () => {
133
134 const {id} = version;
135
136 mockRest.addHandler('fetch', ({baseUrl}) => {
137 expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions`);
138 return {results: [version]};
139 });
140
141 return VersionsPageActionHelper.fetchVersions(dispatch, {
142 itemType: versionPageItemTypes.SOFTWARE_PRODUCT,
143 itemId: id
144 })
145 .then(() => {
146 const {versionsPage: {versionsList}} = store.getState();
147 expect(versionsList.versions[0].id).toEqual(id);
148 expect(versionsList.itemId).toEqual(id);
149 expect(versionsList.itemType).toEqual(versionPageItemTypes.SOFTWARE_PRODUCT);
150 });
151
152 });
153
154});