blob: a397197773ff0b24084db78f355c05c184de98f0 [file] [log] [blame]
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03001/*
2 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
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 *
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030012 * 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.
AviZi280f8012017-06-09 02:39:56 +030015 */
16
17import React from 'react';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030018import { mount } from 'enzyme';
19import { cloneAndSet } from 'test-utils/Util.js';
20import ActivityLogView, {
21 ActivityListItem
22} from 'sdc-app/common/activity-log/ActivityLogView.jsx';
AviZi280f8012017-06-09 02:39:56 +030023import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
Avi Zivb8e2faf2017-07-18 19:45:38 +030024import ActivityLogActionHelper from 'sdc-app/common/activity-log/ActivityLogActionHelper.js';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030025import { mapStateToProps } from 'sdc-app/common/activity-log/ActivityLog.js';
26import { storeCreator } from 'sdc-app/AppStore.js';
AviZi280f8012017-06-09 02:39:56 +030027import mockRest from 'test-utils/MockRest.js';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030028import { ActivityLogStoreFactory } from 'test-utils/factories/activity-log/ActivityLogFactories.js';
talig8e9c0652017-12-20 14:30:43 +020029import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030030import { UserFactory } from 'test-utils/factories/users/UsersFactories.js';
talig8e9c0652017-12-20 14:30:43 +020031
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030032import { actionTypes as userActionTypes } from 'sdc-app/onboarding/users/UsersConstants.js';
AviZi280f8012017-06-09 02:39:56 +030033
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030034describe('Activity Log Module Tests', function() {
35 const LICENSE_MODEL_ID = '555';
36 const version = VersionFactory.build();
37 const usersList = UserFactory.buildList(3);
AviZi280f8012017-06-09 02:39:56 +030038
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030039 it('mapStateToProps mapper exists', () => {
40 expect(mapStateToProps).toBeTruthy();
41 });
AviZi280f8012017-06-09 02:39:56 +030042
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030043 it('Loads Activity Log and renders into jsx', () => {
44 const store = storeCreator();
45 const dispatch = store.dispatch;
46 dispatch({
47 type: userActionTypes.USERS_LIST_LOADED,
48 usersList
49 });
50 let ActivityLogList = ActivityLogStoreFactory.buildList(1, {
51 user: usersList[0].userId
52 });
53 const expectedStore = cloneAndSet(
54 store.getState(),
55 'licenseModel.activityLog',
56 ActivityLogList
57 );
AviZi280f8012017-06-09 02:39:56 +030058
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030059 mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
60 expect(baseUrl).toEqual(
61 `/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${
62 version.id
63 }/activity-logs`
64 );
65 expect(data).toEqual(undefined);
66 expect(options).toEqual(undefined);
67 return { results: ActivityLogList };
68 });
AviZi280f8012017-06-09 02:39:56 +030069
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030070 return ActivityLogActionHelper.fetchActivityLog(dispatch, {
71 itemId: LICENSE_MODEL_ID,
72 versionId: version.id
73 }).then(() => {
74 const state = store.getState();
75 expect(state).toEqual(expectedStore);
76 const props = mapStateToProps(state);
77 expect(props.activities).toEqual(
78 ActivityLogList.map(activity => ({
79 ...activity,
80 user: {
81 id: activity.user,
82 name: usersList.find(
83 userObject => userObject.userId === activity.user
84 ).fullName
85 }
86 }))
87 );
88 const wrapper = mount(<ActivityLogView {...props} />);
89 expect(wrapper).toBeTruthy();
90 });
91 });
AviZi280f8012017-06-09 02:39:56 +030092
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030093 it('Tests Activity Log filter and sorting abilities', () => {
94 const firstDate = new Date();
95 const secondDate = new Date();
96 secondDate.setDate(firstDate.getDate() - 1);
AviZi280f8012017-06-09 02:39:56 +030097
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030098 const firstTimestamp = firstDate.getTime();
99 const secondTimestamp = secondDate.getTime();
AviZi280f8012017-06-09 02:39:56 +0300100
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300101 let firstActivity = ActivityLogStoreFactory.build({
102 user: usersList[0].userId,
103 timestamp: firstTimestamp
104 });
105 let secondActivity = ActivityLogStoreFactory.build({
106 user: usersList[1].userId,
107 timestamp: secondTimestamp,
108 status: { success: false, message: 'error' }
109 });
110 let props = mapStateToProps({
111 users: { usersList },
112 licenseModel: { activityLog: [firstActivity, secondActivity] }
113 });
114 const wrapper = mount(<ActivityLogView {...props} />);
115 expect(wrapper.find(ActivityListItem).length).toEqual(3); // Includes Header component
AviZi280f8012017-06-09 02:39:56 +0300116
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300117 const firstInstance = wrapper.find(ActivityListItem).at(1);
118 const firstInstanceProps = firstInstance.props();
119 expect(firstInstanceProps.activity.timestamp).toEqual(secondTimestamp); // Default sorting is descending
AviZi280f8012017-06-09 02:39:56 +0300120
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300121 const header = wrapper.find(ActivityListItem).at(0);
AviZi280f8012017-06-09 02:39:56 +0300122
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300123 header.props().onSort();
124 wrapper.update();
125 const newFirstInstance = wrapper.find(ActivityListItem).at(1);
126 const newFirstInstanceProps = newFirstInstance.props();
127 expect(newFirstInstanceProps.activity.timestamp).toEqual(
128 firstTimestamp
129 );
130
131 const listEditor = wrapper.find(ListEditorView);
132 listEditor.props().onFilter(usersList[1].fullName);
133 wrapper.update();
134 expect(wrapper.find(ActivityListItem).length).toEqual(2);
135 expect(
136 wrapper
137 .find(ActivityListItem)
138 .at(1)
139 .props().activity.user.name
140 ).toEqual(usersList[1].fullName);
141 });
AviZi280f8012017-06-09 02:39:56 +0300142});