Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2016-2018 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 | * |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 12 | * 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. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | import React from 'react'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 18 | import { mount } from 'enzyme'; |
| 19 | import { cloneAndSet } from 'test-utils/Util.js'; |
| 20 | import ActivityLogView, { |
| 21 | ActivityListItem |
| 22 | } from 'sdc-app/common/activity-log/ActivityLogView.jsx'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx'; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 24 | import ActivityLogActionHelper from 'sdc-app/common/activity-log/ActivityLogActionHelper.js'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 25 | import { mapStateToProps } from 'sdc-app/common/activity-log/ActivityLog.js'; |
| 26 | import { storeCreator } from 'sdc-app/AppStore.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 27 | import mockRest from 'test-utils/MockRest.js'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 28 | import { ActivityLogStoreFactory } from 'test-utils/factories/activity-log/ActivityLogFactories.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 29 | import VersionFactory from 'test-utils/factories/common/VersionFactory.js'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 30 | import { UserFactory } from 'test-utils/factories/users/UsersFactories.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 31 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 32 | import { actionTypes as userActionTypes } from 'sdc-app/onboarding/users/UsersConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 33 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 34 | describe('Activity Log Module Tests', function() { |
| 35 | const LICENSE_MODEL_ID = '555'; |
| 36 | const version = VersionFactory.build(); |
| 37 | const usersList = UserFactory.buildList(3); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 38 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 39 | it('mapStateToProps mapper exists', () => { |
| 40 | expect(mapStateToProps).toBeTruthy(); |
| 41 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 42 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 43 | 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 | ); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 58 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 59 | 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 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 69 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 70 | 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 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 92 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 93 | it('Tests Activity Log filter and sorting abilities', () => { |
| 94 | const firstDate = new Date(); |
| 95 | const secondDate = new Date(); |
| 96 | secondDate.setDate(firstDate.getDate() - 1); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 97 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 98 | const firstTimestamp = firstDate.getTime(); |
| 99 | const secondTimestamp = secondDate.getTime(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 100 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 101 | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 116 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 117 | const firstInstance = wrapper.find(ActivityListItem).at(1); |
| 118 | const firstInstanceProps = firstInstance.props(); |
| 119 | expect(firstInstanceProps.activity.timestamp).toEqual(secondTimestamp); // Default sorting is descending |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 120 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 121 | const header = wrapper.find(ActivityListItem).at(0); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 122 | |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame^] | 123 | 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 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 142 | }); |