Store the namespaced bucket storage for each instance that was created with MockStorage.create() instead of global variable
Signed-off-by: Dominik Henneke <dominik.henneke@sda-se.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/test-utils': patch
|
||||
---
|
||||
|
||||
Store the namespaced bucket storage for each instance that was created with `MockStorage.create()` instead of global variable.
|
||||
@@ -13,8 +13,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { MockStorageApi } from './MockStorageApi';
|
||||
import { StorageApi } from '@backstage/core-plugin-api';
|
||||
import { MockStorageApi } from './MockStorageApi';
|
||||
|
||||
describe('WebStorage Storage API', () => {
|
||||
const createMockStorage = (): StorageApi => {
|
||||
@@ -124,7 +124,7 @@ describe('WebStorage Storage API', () => {
|
||||
expect(secondStorage.get(keyName)).toBe('deerp');
|
||||
});
|
||||
|
||||
it('should not clash with other namesapces when creating buckets', async () => {
|
||||
it('should not clash with other namespaces when creating buckets', async () => {
|
||||
const rootStorage = createMockStorage();
|
||||
|
||||
// when getting key test2 it will translate to /profile/something/deep/test2
|
||||
@@ -139,4 +139,17 @@ describe('WebStorage Storage API', () => {
|
||||
|
||||
expect(secondStorage.get('deep/test2')).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should not reuse storage instances between different rootStorages', async () => {
|
||||
const rootStorage1 = createMockStorage();
|
||||
const rootStorage2 = createMockStorage();
|
||||
|
||||
const firstStorage = rootStorage1.forBucket('something');
|
||||
const secondStorage = rootStorage2.forBucket('something');
|
||||
|
||||
await firstStorage.set('test2', true);
|
||||
|
||||
expect(firstStorage.get('test2')).toBe(true);
|
||||
expect(secondStorage.get('test2')).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,29 +23,37 @@ import ObservableImpl from 'zen-observable';
|
||||
|
||||
export type MockStorageBucket = { [key: string]: any };
|
||||
|
||||
const bucketStorageApis = new Map<string, MockStorageApi>();
|
||||
|
||||
export class MockStorageApi implements StorageApi {
|
||||
private readonly namespace: string;
|
||||
private readonly data: MockStorageBucket;
|
||||
private readonly bucketStorageApis: Map<string, MockStorageApi>;
|
||||
|
||||
private constructor(namespace: string, data?: MockStorageBucket) {
|
||||
private constructor(
|
||||
namespace: string,
|
||||
bucketStorageApis: Map<string, MockStorageApi>,
|
||||
data?: MockStorageBucket,
|
||||
) {
|
||||
this.namespace = namespace;
|
||||
this.bucketStorageApis = bucketStorageApis;
|
||||
this.data = { ...data };
|
||||
}
|
||||
|
||||
static create(data?: MockStorageBucket) {
|
||||
return new MockStorageApi('', data);
|
||||
return new MockStorageApi('', new Map(), data);
|
||||
}
|
||||
|
||||
forBucket(name: string): StorageApi {
|
||||
if (!bucketStorageApis.has(name)) {
|
||||
bucketStorageApis.set(
|
||||
if (!this.bucketStorageApis.has(name)) {
|
||||
this.bucketStorageApis.set(
|
||||
name,
|
||||
new MockStorageApi(`${this.namespace}/${name}`, this.data),
|
||||
new MockStorageApi(
|
||||
`${this.namespace}/${name}`,
|
||||
this.bucketStorageApis,
|
||||
this.data,
|
||||
),
|
||||
);
|
||||
}
|
||||
return bucketStorageApis.get(name)!;
|
||||
return this.bucketStorageApis.get(name)!;
|
||||
}
|
||||
|
||||
get<T>(key: string): T | undefined {
|
||||
|
||||
Reference in New Issue
Block a user