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 15import {PrimaryTrackSortKey} from '../public'; 16 17import {createEmptyState} from './empty_state'; 18import {getContainingGroupKey, State} from './state'; 19import {deserializeStateObject, serializeStateObject} from './upload_utils'; 20 21test('createEmptyState', () => { 22 const state: State = createEmptyState(); 23 expect(state.engine).toEqual(undefined); 24}); 25 26test('getContainingTrackId', () => { 27 const state: State = createEmptyState(); 28 state.tracks['a'] = { 29 key: 'a', 30 uri: 'Foo', 31 name: 'a track', 32 trackSortKey: PrimaryTrackSortKey.ORDINARY_TRACK, 33 }; 34 35 state.tracks['b'] = { 36 key: 'b', 37 uri: 'Foo', 38 name: 'b track', 39 trackSortKey: PrimaryTrackSortKey.ORDINARY_TRACK, 40 trackGroup: 'containsB', 41 }; 42 43 expect(getContainingGroupKey(state, 'z')).toEqual(null); 44 expect(getContainingGroupKey(state, 'a')).toEqual(null); 45 expect(getContainingGroupKey(state, 'b')).toEqual('containsB'); 46}); 47 48test('state is serializable', () => { 49 const state = createEmptyState(); 50 const json = serializeStateObject(state); 51 const restored = deserializeStateObject<State>(json); 52 53 // Remove non-serialized fields from the original state object, so it may be 54 // compared fairly with the restored version. 55 // This is a legitimate use of 'any'. We are comparing this object against 56 // one that's taken a round trip through JSON, which has therefore lost any 57 // type information. Attempting to ask TS for help here would serve no 58 // purpose. 59 // eslint-disable-next-line @typescript-eslint/no-explicit-any 60 const serializableState: any = state; 61 serializableState.nonSerializableState = undefined; 62 63 // Remove any undefined values from original as JSON doesn't serialize them 64 for (const key in serializableState) { 65 if (serializableState[key] === undefined) { 66 delete serializableState[key]; 67 } 68 } 69 70 expect(serializableState).toEqual(restored); 71}); 72