diff --git a/pkg/webui/console/store/reducers/tests/events_test.js b/pkg/webui/console/store/reducers/tests/events_test.js deleted file mode 100644 index c82a74f6c0..0000000000 --- a/pkg/webui/console/store/reducers/tests/events_test.js +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright © 2020 The Things Network Foundation, The Things Industries B.V. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import createReducer from '../events' -import { getEventMessageSuccess } from '../../actions/events' - -describe('Events reducer', () => { - const testId = 'test' - const reducer = createReducer(testId) - const successActionCreator = getEventMessageSuccess(testId) - - describe('when adding new events', () => { - it('keeps events sorted by `time`', () => { - const testId = 'test' - const testTime = '2019-03-28T13:18:13.376022Z' - const testDate = new Date(testTime) - - const initialState = reducer(undefined, {}) - - const event1 = { time: testTime } - let newState = reducer(initialState, successActionCreator(testId, event1)) - - expect(newState[testId].events).toHaveLength(1) - expect(newState[testId].events[0]).toEqual(event1) - - const event2 = { time: new Date(testDate.getTime() + 1000).toISOString() } - newState = reducer(newState, successActionCreator(testId, event2)) - - expect(newState[testId].events).toHaveLength(2) - expect(newState[testId].events[0]).toEqual(event2) - expect(newState[testId].events[1]).toEqual(event1) - - const event3 = { time: new Date(testDate.getTime() - 1000).toISOString() } - newState = reducer(newState, successActionCreator(testId, event3)) - - expect(newState[testId].events).toHaveLength(3) - expect(newState[testId].events[0]).toEqual(event2) - expect(newState[testId].events[1]).toEqual(event1) - expect(newState[testId].events[2]).toEqual(event3) - - const event4 = { time: new Date(testDate.getTime() + 2000).toISOString() } - newState = reducer(newState, successActionCreator(testId, event4)) - - expect(newState[testId].events).toHaveLength(4) - expect(newState[testId].events[0]).toEqual(event4) - expect(newState[testId].events[1]).toEqual(event2) - expect(newState[testId].events[2]).toEqual(event1) - expect(newState[testId].events[3]).toEqual(event3) - - const event5 = { time: new Date(testDate.getTime()).toISOString() } - newState = reducer(newState, successActionCreator(testId, event5)) - - expect(newState[testId].events).toHaveLength(5) - expect(newState[testId].events[0]).toEqual(event4) - expect(newState[testId].events[1]).toEqual(event2) - expect(newState[testId].events[2]).toEqual(event5) - expect(newState[testId].events[3]).toEqual(event1) - expect(newState[testId].events[4]).toEqual(event3) - }) - }) -}) diff --git a/pkg/webui/console/store/reducers/tests/gateway-status_test.js b/pkg/webui/console/store/reducers/tests/gateway-status_test.js index aa85d43e20..b06e2b201f 100644 --- a/pkg/webui/console/store/reducers/tests/gateway-status_test.js +++ b/pkg/webui/console/store/reducers/tests/gateway-status_test.js @@ -16,9 +16,16 @@ import reducer, { defaultState } from '../gateway-status' import { getGateway, updateGatewayStatisticsSuccess, - getGatewayEventMessageSuccess, + getGatewayEventMessagesSuccess, } from '../../actions/gateways' +const genericEvent = { + identifiers: [{ gateway_ids: { gateway_id: 'test-gateway-id' } }], + name: 'gateway.update', + time: '2020-04-23T09:41:04.134034132Z', + unique_id: '01EK2PTJ6J42DKWGC2F3PZ32TM', +} + describe('Gateway-status reducer', () => { it('returns the initial state', () => { expect(reducer(undefined, { type: '@@TEST_INIT', payload: {} })).toEqual(defaultState) @@ -118,7 +125,7 @@ describe('Gateway-status reducer', () => { time: '2019-09-24T13:40:30.033728431Z', } - newState = reducer(defaultState, getGatewayEventMessageSuccess('test-gtw-id', event)) + newState = reducer(defaultState, getGatewayEventMessagesSuccess('test-gtw-id', [event])) expect(newState !== defaultState).toBe(true) expect(newState.lastSeen).toBeDefined() expect(newState.lastSeen).toStrictEqual(new Date(event.time)) @@ -130,7 +137,22 @@ describe('Gateway-status reducer', () => { time: '2019-09-24T13:45:30.033728431Z', } - newState = reducer(newState, getGatewayEventMessageSuccess('test-gtw-id', event)) + newState = reducer(newState, getGatewayEventMessagesSuccess('test-gtw-id', [event])) + expect(newState !== defaultState).toBe(true) + expect(newState.lastSeen).toBeDefined() + expect(newState.lastSeen).toStrictEqual(new Date(event.time)) + }) + + it('updates `lastSeen` on most recent uplink event when dispatching multiple events', () => { + const event = { + name: 'gs.up.receive', + time: '2019-09-24T13:50:30.033728431Z', + } + + newState = reducer( + newState, + getGatewayEventMessagesSuccess('test-gtw-id', [genericEvent, event]), + ) expect(newState !== defaultState).toBe(true) expect(newState.lastSeen).toBeDefined() expect(newState.lastSeen).toStrictEqual(new Date(event.time)) @@ -142,7 +164,7 @@ describe('Gateway-status reducer', () => { time: '2019-09-24T13:30:30.033728431Z', } - newState = reducer(newState, getGatewayEventMessageSuccess('test-gtw-id', event)) + newState = reducer(newState, getGatewayEventMessagesSuccess('test-gtw-id', [event])) expect(newState !== defaultState).toBe(true) expect(newState.lastSeen).toBeDefined() expect(newState.lastSeen).not.toStrictEqual(new Date(event.time)) @@ -154,7 +176,7 @@ describe('Gateway-status reducer', () => { time: '2019-09-24T13:50:30.033728431Z', } - newState = reducer(newState, getGatewayEventMessageSuccess('test-gtw-id', event)) + newState = reducer(newState, getGatewayEventMessagesSuccess('test-gtw-id', [event])) expect(newState !== defaultState).toBe(true) expect(newState.lastSeen).toBeDefined() expect(newState.lastSeen).toStrictEqual(new Date(event.time)) @@ -166,7 +188,22 @@ describe('Gateway-status reducer', () => { time: '2019-09-24T13:55:30.033728431Z', } - newState = reducer(newState, getGatewayEventMessageSuccess('test-gtw-id', event)) + newState = reducer(newState, getGatewayEventMessagesSuccess('test-gtw-id', [event])) + expect(newState !== defaultState).toBe(true) + expect(newState.lastSeen).toBeDefined() + expect(newState.lastSeen).toStrictEqual(new Date(event.time)) + }) + + it('updates `lastSeen` on most recent status event when dispatching multiple events', () => { + const event = { + name: 'gs.status.receive', + time: '2019-09-24T13:59:30.033728431Z', + } + + newState = reducer( + newState, + getGatewayEventMessagesSuccess('test-gtw-id', [genericEvent, event]), + ) expect(newState !== defaultState).toBe(true) expect(newState.lastSeen).toBeDefined() expect(newState.lastSeen).toStrictEqual(new Date(event.time)) @@ -178,7 +215,7 @@ describe('Gateway-status reducer', () => { time: '2019-09-24T13:35:30.033728431Z', } - newState = reducer(newState, getGatewayEventMessageSuccess('test-gtw-id', event)) + newState = reducer(newState, getGatewayEventMessagesSuccess('test-gtw-id', [event])) expect(newState !== defaultState).toBe(true) expect(newState.lastSeen).toBeDefined() expect(newState.lastSeen).not.toStrictEqual(new Date(event.time))