AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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'; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 20 | import ActivityLogView, {ActivityListItem} from 'sdc-app/common/activity-log/ActivityLogView.jsx'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 21 | import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx'; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 22 | import ActivityLogActionHelper from 'sdc-app/common/activity-log/ActivityLogActionHelper.js'; |
| 23 | import {mapStateToProps} from 'sdc-app/common/activity-log/ActivityLog.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 27 | import VersionFactory from 'test-utils/factories/common/VersionFactory.js'; |
| 28 | import {UserFactory} from 'test-utils/factories/users/UsersFactories.js'; |
| 29 | |
| 30 | import {actionTypes as userActionTypes} from 'sdc-app/onboarding/users/UsersConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 31 | |
| 32 | describe('Activity Log Module Tests', function () { |
| 33 | const LICENSE_MODEL_ID = '555'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 34 | const version = VersionFactory.build(); |
| 35 | const usersList = UserFactory.buildList(3); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 36 | |
| 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; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 44 | dispatch({ |
| 45 | type: userActionTypes.USERS_LIST_LOADED, |
| 46 | usersList |
| 47 | }); |
| 48 | let ActivityLogList = ActivityLogStoreFactory.buildList(1, {user: usersList[0].userId}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 49 | const expectedStore = cloneAndSet(store.getState(), 'licenseModel.activityLog', ActivityLogList); |
| 50 | |
| 51 | mockRest.addHandler('fetch', ({data, options, baseUrl}) => { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 52 | expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}/activity-logs`); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 53 | 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); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 62 | expect(props.activities).toEqual(ActivityLogList.map(activity => |
| 63 | ({...activity, user: {id: activity.user, name: usersList.find(userObject => userObject.userId === activity.user).fullName}}) |
| 64 | )); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 65 | 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 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 78 | 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]}}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 81 | 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); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 95 | listEditor.props().onFilter(usersList[1].fullName); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 96 | expect(wrapper.find(ActivityListItem).length).toEqual(2); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 97 | expect(wrapper.find(ActivityListItem).at(1).props().activity.user.name).toEqual(usersList[1].fullName); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 98 | }); |
| 99 | }); |