• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import {InMemoryStorage} from 'common/in_memory_storage';
18 import {PersistentStoreProxy} from './persistent_store_proxy';
19 
20 describe('PersistentStoreObject', () => {
21   it('uses defaults when no store is available', () => {
22     const mockStorage = new InMemoryStorage();
23 
24     const defaultValues = {
25       key1: 'value',
26       key2: true,
27     };
28     const storeObject = PersistentStoreProxy.new(
29       'storeKey',
30       defaultValues,
31       mockStorage,
32     );
33 
34     expect(storeObject['key1']).toBe('value');
35     expect(storeObject['key2']).toBe(true);
36   });
37 
38   it('can update properties', () => {
39     const mockStorage = new InMemoryStorage();
40 
41     const defaultValues = {
42       key1: 'value',
43       key2: true,
44     };
45     const storeObject = PersistentStoreProxy.new(
46       'storeKey',
47       defaultValues,
48       mockStorage,
49     );
50 
51     storeObject['key1'] = 'someOtherValue';
52     storeObject['key2'] = false;
53     expect(storeObject['key1']).toBe('someOtherValue');
54     expect(storeObject['key2']).toBe(false);
55   });
56 
57   it('uses explicitly set store data', () => {
58     const mockStorage = new InMemoryStorage();
59 
60     const defaultValues = {
61       key1: 'value',
62       key2: true,
63     };
64     const storeObject = PersistentStoreProxy.new(
65       'storeKey',
66       defaultValues,
67       mockStorage,
68     );
69     storeObject['key1'] = 'someOtherValue';
70     storeObject['key2'] = false;
71 
72     const newStoreObject = PersistentStoreProxy.new(
73       'storeKey',
74       defaultValues,
75       mockStorage,
76     );
77     expect(newStoreObject['key1']).toBe('someOtherValue');
78     expect(newStoreObject['key2']).toBe(false);
79   });
80 
81   it('uses default values if not explicitly set', () => {
82     const mockStorage = new InMemoryStorage();
83 
84     const defaultValues = {
85       key1: 'value',
86       key2: true,
87     };
88     const storeObject = PersistentStoreProxy.new(
89       'storeKey',
90       defaultValues,
91       mockStorage,
92     );
93     expect(storeObject['key1']).toBe('value');
94     expect(storeObject['key2']).toBe(true);
95 
96     const newDefaultValues = {
97       key1: 'someOtherValue',
98       key2: false,
99     };
100     const newStoreObject = PersistentStoreProxy.new(
101       'storeKey',
102       newDefaultValues,
103       mockStorage,
104     );
105     expect(newStoreObject['key1']).toBe('someOtherValue');
106     expect(newStoreObject['key2']).toBe(false);
107   });
108 
109   it("can't update non leaf configs", () => {
110     const mockStorage = new InMemoryStorage();
111 
112     const defaultValues = {
113       key1: 'value',
114       key2: {
115         key3: true,
116       },
117     };
118     const storeObject = PersistentStoreProxy.new(
119       'storeKey',
120       defaultValues,
121       mockStorage,
122     );
123     expect(() => (storeObject['key2'] = {key3: false})).toThrow();
124   });
125 
126   it('can get nested configs', () => {
127     const mockStorage = new InMemoryStorage();
128 
129     const defaultValues = {
130       key1: 'value',
131       key2: {
132         key3: true,
133       },
134     };
135     const storeObject = PersistentStoreProxy.new(
136       'storeKey',
137       defaultValues,
138       mockStorage,
139     );
140     expect(storeObject['key2']['key3']).toBe(true);
141   });
142 
143   it('can update schema', () => {
144     const mockStorage = new InMemoryStorage();
145 
146     const schema1 = {
147       key1: 'value1',
148       key2: {
149         key3: true,
150       },
151     };
152     const storeObject1 = PersistentStoreProxy.new(
153       'storeKey',
154       schema1,
155       mockStorage,
156     );
157     expect(storeObject1['key1']).toBe('value1');
158     expect(storeObject1['key2']['key3']).toBe(true);
159 
160     // Change from default value to ensure we update the storage
161     storeObject1['key1'] = 'someOtherValue';
162     storeObject1['key2']['key3'] = false;
163 
164     const schema2 = {
165       key1: {
166         key3: 'value2',
167       },
168       key2: true,
169     };
170     const storeObject2 = PersistentStoreProxy.new(
171       'storeKey',
172       schema2,
173       mockStorage,
174     );
175     expect(storeObject2['key1']['key3']).toBe('value2');
176     expect(storeObject2['key2']).toBe(true);
177   });
178 });
179