diff --git a/.changeset/stupid-elephants-invent.md b/.changeset/stupid-elephants-invent.md new file mode 100644 index 0000000000..11b87a667d --- /dev/null +++ b/.changeset/stupid-elephants-invent.md @@ -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. diff --git a/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.test.ts b/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.test.ts index fabfa989e2..a3028f8b57 100644 --- a/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.test.ts +++ b/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.test.ts @@ -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); + }); }); diff --git a/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.ts b/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.ts index 9f21b4e4c8..ab5d26afc5 100644 --- a/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.ts +++ b/packages/test-utils/src/testUtils/apis/StorageApi/MockStorageApi.ts @@ -23,29 +23,37 @@ import ObservableImpl from 'zen-observable'; export type MockStorageBucket = { [key: string]: any }; -const bucketStorageApis = new Map(); - export class MockStorageApi implements StorageApi { private readonly namespace: string; private readonly data: MockStorageBucket; + private readonly bucketStorageApis: Map; - private constructor(namespace: string, data?: MockStorageBucket) { + private constructor( + namespace: string, + bucketStorageApis: Map, + 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(key: string): T | undefined {