Hector Dearman | e0f20f1 | 2018-07-25 13:20:44 +0100 | [diff] [blame] | 1 | // Copyright (C) 2018 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import {createEmptyState} from '../common/state'; |
| 16 | import {rootReducer} from './reducer'; |
| 17 | |
Hector Dearman | a38bd90 | 2018-08-02 10:38:41 +0100 | [diff] [blame] | 18 | test('navigate', async () => { |
Hector Dearman | e0f20f1 | 2018-07-25 13:20:44 +0100 | [diff] [blame] | 19 | const before = createEmptyState(); |
Hector Dearman | a38bd90 | 2018-08-02 10:38:41 +0100 | [diff] [blame] | 20 | const after = rootReducer(before, {type: 'NAVIGATE', route: '/foo'}); |
| 21 | expect(after.route).toBe('/foo'); |
Hector Dearman | e0f20f1 | 2018-07-25 13:20:44 +0100 | [diff] [blame] | 22 | }); |
Michail Schwab | 1cc1aa0 | 2018-08-02 14:45:35 -0400 | [diff] [blame] | 23 | |
| 24 | test('add tracks', () => { |
| 25 | const empty = createEmptyState(); |
| 26 | const step1 = rootReducer(empty, { |
| 27 | type: 'ADD_TRACK', |
| 28 | engineId: '1', |
| 29 | trackKind: 'cpu', |
| 30 | cpu: '1', |
| 31 | }); |
| 32 | const state = rootReducer(step1, { |
| 33 | type: 'ADD_TRACK', |
| 34 | engineId: '2', |
| 35 | trackKind: 'cpu', |
| 36 | cpu: '2', |
| 37 | }); |
| 38 | expect(Object.values(state.tracks).length).toBe(2); |
| 39 | expect(state.displayedTrackIds.length).toBe(2); |
| 40 | }); |
| 41 | |
| 42 | test('reorder tracks', () => { |
| 43 | const empty = createEmptyState(); |
| 44 | const step1 = rootReducer(empty, { |
| 45 | type: 'ADD_TRACK', |
| 46 | engineId: '1', |
| 47 | trackKind: 'cpu', |
| 48 | cpu: '1', |
| 49 | }); |
| 50 | const before = rootReducer(step1, { |
| 51 | type: 'ADD_TRACK', |
| 52 | engineId: '2', |
| 53 | trackKind: 'cpu', |
| 54 | cpu: '2', |
| 55 | }); |
| 56 | |
| 57 | const firstTrackId = before.displayedTrackIds[0]; |
| 58 | const secondTrackId = before.displayedTrackIds[1]; |
| 59 | |
| 60 | const after = rootReducer(before, { |
| 61 | type: 'MOVE_TRACK', |
| 62 | trackId: `${firstTrackId}`, |
| 63 | direction: 'down', |
| 64 | }); |
| 65 | |
| 66 | // Ensure the order is swapped. This test would fail to detect side effects |
| 67 | // if the before state was modified, so other tests are needed as well. |
| 68 | expect(after.displayedTrackIds[0]).toBe(secondTrackId); |
| 69 | expect(after.displayedTrackIds[1]).toBe(firstTrackId); |
| 70 | |
| 71 | // Ensure the track state contents have actually swapped places in the new |
| 72 | // state, but not in the old one. |
| 73 | expect(before.tracks[before.displayedTrackIds[0]].engineId).toBe('1'); |
| 74 | expect(before.tracks[before.displayedTrackIds[1]].engineId).toBe('2'); |
| 75 | expect(after.tracks[after.displayedTrackIds[0]].engineId).toBe('2'); |
| 76 | expect(after.tracks[after.displayedTrackIds[1]].engineId).toBe('1'); |
Hector Dearman | f9f2db0 | 2018-08-03 13:09:32 +0100 | [diff] [blame] | 77 | }); |
| 78 | |
| 79 | test('open trace', async () => { |
| 80 | const before = createEmptyState(); |
| 81 | const after = rootReducer(before, { |
Primiano Tucci | 8afc06d | 2018-08-06 19:11:42 +0100 | [diff] [blame] | 82 | type: 'OPEN_TRACE_FROM_URL', |
Hector Dearman | f9f2db0 | 2018-08-03 13:09:32 +0100 | [diff] [blame] | 83 | url: 'https://example.com/bar', |
| 84 | }); |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 85 | const engineKeys = Object.keys(after.engines); |
| 86 | expect(engineKeys.length).toBe(1); |
| 87 | expect(after.engines[engineKeys[0]].source).toBe('https://example.com/bar'); |
Hector Dearman | f9f2db0 | 2018-08-03 13:09:32 +0100 | [diff] [blame] | 88 | expect(after.route).toBe('/viewer'); |
| 89 | }); |
Hector Dearman | 03f962c | 2018-08-09 17:00:32 +0100 | [diff] [blame] | 90 | |
| 91 | test('set state', async () => { |
| 92 | const newState = createEmptyState(); |
| 93 | const before = createEmptyState(); |
| 94 | const after = rootReducer(before, { |
| 95 | type: 'SET_STATE', |
| 96 | newState, |
| 97 | }); |
| 98 | expect(after).toBe(newState); |
| 99 | }); |