blob: 39487399b369f0410e86678690b9e2203842db47 [file] [log] [blame]
Hector Dearmane0f20f12018-07-25 13:20:44 +01001// 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
15import {createEmptyState} from '../common/state';
16import {rootReducer} from './reducer';
17
Hector Dearmana38bd902018-08-02 10:38:41 +010018test('navigate', async () => {
Hector Dearmane0f20f12018-07-25 13:20:44 +010019 const before = createEmptyState();
Hector Dearmana38bd902018-08-02 10:38:41 +010020 const after = rootReducer(before, {type: 'NAVIGATE', route: '/foo'});
21 expect(after.route).toBe('/foo');
Hector Dearmane0f20f12018-07-25 13:20:44 +010022});
Michail Schwab1cc1aa02018-08-02 14:45:35 -040023
24test('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
42test('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 Dearmanf9f2db02018-08-03 13:09:32 +010077});
78
79test('open trace', async () => {
80 const before = createEmptyState();
81 const after = rootReducer(before, {
Primiano Tucci8afc06d2018-08-06 19:11:42 +010082 type: 'OPEN_TRACE_FROM_URL',
Hector Dearmanf9f2db02018-08-03 13:09:32 +010083 url: 'https://example.com/bar',
84 });
Primiano Tuccie36ca632018-08-21 14:32:23 +020085 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 Dearmanf9f2db02018-08-03 13:09:32 +010088 expect(after.route).toBe('/viewer');
89});
Hector Dearman03f962c2018-08-09 17:00:32 +010090
91test('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});