2f377a3539ff237787cfd393da6b427fc2836d7b
[sdc.git] / openecomp-ui / test / nfvo-components / activity-log / ActivityLog.test.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 import React from 'react';
18 import {mount} from 'enzyme';
19 import {cloneAndSet} from 'test-utils/Util.js';
20 import ActivityLogView, {ActivityListItem} from 'nfvo-components/activity-log/ActivityLogView.jsx';
21 import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
22 import ActivityLogActionHelper from 'nfvo-components/activity-log/ActivityLogActionHelper.js';
23 import {mapStateToProps} from 'nfvo-components/activity-log/ActivityLog.js';
24 import {storeCreator} from 'sdc-app/AppStore.js';
25 import mockRest from 'test-utils/MockRest.js';
26 import {ActivityLogStoreFactory} from 'test-utils/factories/activity-log/ActivityLogFactories.js';
27 import VersionControllerUtilsFactory from 'test-utils/factories/softwareProduct/VersionControllerUtilsFactory.js';
28
29 describe('Activity Log Module Tests', function () {
30         const LICENSE_MODEL_ID = '555';
31         const version = VersionControllerUtilsFactory.build().version;
32
33         it('mapStateToProps mapper exists', () => {
34                 expect(mapStateToProps).toBeTruthy();
35         });
36
37         it('Loads Activity Log and renders into jsx', () => {
38                 const store = storeCreator();
39                 const dispatch = store.dispatch;
40                 let ActivityLogList = ActivityLogStoreFactory.buildList(1);
41                 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.activityLog', ActivityLogList);
42
43                 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
44                         expect(baseUrl).toEqual(`/onboarding-api/v1.0/activity-logs/${LICENSE_MODEL_ID}/versions/${version.id}`);
45                         expect(data).toEqual(undefined);
46                         expect(options).toEqual(undefined);
47                         return {results: ActivityLogList};
48                 });
49
50                 return ActivityLogActionHelper.fetchActivityLog(dispatch, {itemId: LICENSE_MODEL_ID, versionId: version.id}).then(() => {
51                         const state = store.getState();
52                         expect(state).toEqual(expectedStore);
53                         const props = mapStateToProps(state);
54                         expect(props.activities).toEqual(ActivityLogList);
55                         const wrapper = mount(<ActivityLogView {...props}/>);
56                         expect(wrapper).toBeTruthy();
57                 });
58         });
59
60         it('Tests Activity Log filter and sorting abilities', () => {
61                 const firstDate = new Date();
62                 const secondDate = new Date();
63                 secondDate.setDate(firstDate.getDate() - 1);
64
65                 const firstTimestamp = firstDate.getTime();
66                 const secondTimestamp = secondDate.getTime();
67
68                 let firstActivity = ActivityLogStoreFactory.build({user: 'first', timestamp: firstTimestamp});
69                 let secondActivity = ActivityLogStoreFactory.build({user: 'second', timestamp: secondTimestamp, status: {success: false, message: 'error'}});
70                 let props = mapStateToProps({licenseModel: {activityLog: [firstActivity, secondActivity]}});
71                 const wrapper = mount(<ActivityLogView {...props}/>);
72                 expect(wrapper.find(ActivityListItem).length).toEqual(3); // Includes Header component
73
74                 const firstInstance = wrapper.find(ActivityListItem).at(1);
75                 const firstInstanceProps = firstInstance.props();
76                 expect(firstInstanceProps.activity.timestamp).toEqual(secondTimestamp); // Default sorting is descending
77
78                 const header = wrapper.find(ActivityListItem).at(0);
79                 header.props().onSort();
80                 const newFirstInstance = wrapper.find(ActivityListItem).at(1);
81                 const newFirstInstanceProps = newFirstInstance.props();
82                 expect(newFirstInstanceProps.activity.timestamp).toEqual(firstTimestamp);
83
84                 const listEditor = wrapper.find(ListEditorView);
85                 listEditor.props().onFilter('second');
86                 expect(wrapper.find(ActivityListItem).length).toEqual(2);
87                 expect(wrapper.find(ActivityListItem).at(1).props().activity.user).toEqual('second');
88         });
89 });