blob: d2c135eb79f822eafe7fea7e1e3e8f91a7fb774e [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
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
17import React from 'react';
18import {mount} from 'enzyme';
19import {cloneAndSet} from 'test-utils/Util.js';
Avi Zivb8e2faf2017-07-18 19:45:38 +030020import ActivityLogView, {ActivityListItem} from 'sdc-app/common/activity-log/ActivityLogView.jsx';
AviZi280f8012017-06-09 02:39:56 +030021import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
Avi Zivb8e2faf2017-07-18 19:45:38 +030022import ActivityLogActionHelper from 'sdc-app/common/activity-log/ActivityLogActionHelper.js';
23import {mapStateToProps} from 'sdc-app/common/activity-log/ActivityLog.js';
AviZi280f8012017-06-09 02:39:56 +030024import {storeCreator} from 'sdc-app/AppStore.js';
25import mockRest from 'test-utils/MockRest.js';
26import {ActivityLogStoreFactory} from 'test-utils/factories/activity-log/ActivityLogFactories.js';
talig8e9c0652017-12-20 14:30:43 +020027import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
28import {UserFactory} from 'test-utils/factories/users/UsersFactories.js';
29
30import {actionTypes as userActionTypes} from 'sdc-app/onboarding/users/UsersConstants.js';
AviZi280f8012017-06-09 02:39:56 +030031
32describe('Activity Log Module Tests', function () {
33 const LICENSE_MODEL_ID = '555';
talig8e9c0652017-12-20 14:30:43 +020034 const version = VersionFactory.build();
35 const usersList = UserFactory.buildList(3);
AviZi280f8012017-06-09 02:39:56 +030036
37 it('mapStateToProps mapper exists', () => {
38 expect(mapStateToProps).toBeTruthy();
39 });
40
41 it('Loads Activity Log and renders into jsx', () => {
42 const store = storeCreator();
43 const dispatch = store.dispatch;
talig8e9c0652017-12-20 14:30:43 +020044 dispatch({
45 type: userActionTypes.USERS_LIST_LOADED,
46 usersList
47 });
48 let ActivityLogList = ActivityLogStoreFactory.buildList(1, {user: usersList[0].userId});
AviZi280f8012017-06-09 02:39:56 +030049 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.activityLog', ActivityLogList);
50
51 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
talig8e9c0652017-12-20 14:30:43 +020052 expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}/activity-logs`);
AviZi280f8012017-06-09 02:39:56 +030053 expect(data).toEqual(undefined);
54 expect(options).toEqual(undefined);
55 return {results: ActivityLogList};
56 });
57
58 return ActivityLogActionHelper.fetchActivityLog(dispatch, {itemId: LICENSE_MODEL_ID, versionId: version.id}).then(() => {
59 const state = store.getState();
60 expect(state).toEqual(expectedStore);
61 const props = mapStateToProps(state);
talig8e9c0652017-12-20 14:30:43 +020062 expect(props.activities).toEqual(ActivityLogList.map(activity =>
63 ({...activity, user: {id: activity.user, name: usersList.find(userObject => userObject.userId === activity.user).fullName}})
64 ));
AviZi280f8012017-06-09 02:39:56 +030065 const wrapper = mount(<ActivityLogView {...props}/>);
66 expect(wrapper).toBeTruthy();
67 });
68 });
69
70 it('Tests Activity Log filter and sorting abilities', () => {
71 const firstDate = new Date();
72 const secondDate = new Date();
73 secondDate.setDate(firstDate.getDate() - 1);
74
75 const firstTimestamp = firstDate.getTime();
76 const secondTimestamp = secondDate.getTime();
77
talig8e9c0652017-12-20 14:30:43 +020078 let firstActivity = ActivityLogStoreFactory.build({user: usersList[0].userId, timestamp: firstTimestamp});
79 let secondActivity = ActivityLogStoreFactory.build({user: usersList[1].userId, timestamp: secondTimestamp, status: {success: false, message: 'error'}});
80 let props = mapStateToProps({users: {usersList}, licenseModel: {activityLog: [firstActivity, secondActivity]}});
AviZi280f8012017-06-09 02:39:56 +030081 const wrapper = mount(<ActivityLogView {...props}/>);
82 expect(wrapper.find(ActivityListItem).length).toEqual(3); // Includes Header component
83
84 const firstInstance = wrapper.find(ActivityListItem).at(1);
85 const firstInstanceProps = firstInstance.props();
86 expect(firstInstanceProps.activity.timestamp).toEqual(secondTimestamp); // Default sorting is descending
87
88 const header = wrapper.find(ActivityListItem).at(0);
89 header.props().onSort();
90 const newFirstInstance = wrapper.find(ActivityListItem).at(1);
91 const newFirstInstanceProps = newFirstInstance.props();
92 expect(newFirstInstanceProps.activity.timestamp).toEqual(firstTimestamp);
93
94 const listEditor = wrapper.find(ListEditorView);
talig8e9c0652017-12-20 14:30:43 +020095 listEditor.props().onFilter(usersList[1].fullName);
AviZi280f8012017-06-09 02:39:56 +030096 expect(wrapper.find(ActivityListItem).length).toEqual(2);
talig8e9c0652017-12-20 14:30:43 +020097 expect(wrapper.find(ActivityListItem).at(1).props().activity.user.name).toEqual(usersList[1].fullName);
AviZi280f8012017-06-09 02:39:56 +030098 });
99});