mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 09:32:41 +00:00
test: prevent error from leaking
This commit is contained in:
@@ -22,20 +22,22 @@ import {StorageProvider} from './storage.provider';
|
|||||||
describe('StorageProvider', () => {
|
describe('StorageProvider', () => {
|
||||||
let storageProvider: StorageProvider;
|
let storageProvider: StorageProvider;
|
||||||
let storage: Storage;
|
let storage: Storage;
|
||||||
|
let sampleEntries: Map<string, any>;
|
||||||
|
|
||||||
const sampleEntries: Map<string, any> = new Map([
|
beforeEach(() => {
|
||||||
['foo', 'Bar'],
|
|
||||||
['bar', {foo: 'BarFoo'} as any],
|
|
||||||
['foo.bar', 123],
|
|
||||||
]);
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [StorageModule],
|
imports: [StorageModule],
|
||||||
providers: [StorageProvider],
|
providers: [StorageProvider],
|
||||||
});
|
});
|
||||||
storageProvider = TestBed.get(StorageProvider);
|
storageProvider = TestBed.get(StorageProvider);
|
||||||
storage = TestBed.get(Storage);
|
storage = TestBed.get(Storage);
|
||||||
|
|
||||||
|
sampleEntries = new Map([
|
||||||
|
['foo', 'Bar'],
|
||||||
|
['bar', {foo: 'BarFoo'} as any],
|
||||||
|
['foo.bar', 123],
|
||||||
|
]);
|
||||||
|
|
||||||
spyOn(storage, 'forEach').and.callFake(function_ => {
|
spyOn(storage, 'forEach').and.callFake(function_ => {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for (const key of sampleEntries.keys()) {
|
for (const key of sampleEntries.keys()) {
|
||||||
@@ -48,23 +50,28 @@ describe('StorageProvider', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call ready method of storage on init', () => {
|
it('should call ready method of storage on init', async () => {
|
||||||
spyOn(storage, 'create');
|
// @ts-ignore
|
||||||
storageProvider.init();
|
spyOn(storage, 'create').and.callFake(() => Promise.resolve(undefined));
|
||||||
|
await storageProvider.init();
|
||||||
|
|
||||||
expect(storage.create).toHaveBeenCalled();
|
expect(storage.create).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call set method of storage to put a value', () => {
|
it('should call set method of storage to put a value', async () => {
|
||||||
spyOn(storage, 'set');
|
spyOn(storage, 'set').and.callFake(() => Promise.resolve());
|
||||||
storageProvider.put('some-uid', {some: 'thing'});
|
await storageProvider.put('some-uid', {some: 'thing'});
|
||||||
|
|
||||||
expect(storage.set).toHaveBeenCalledWith('some-uid', {some: 'thing'});
|
expect(storage.set).toHaveBeenCalledWith('some-uid', {some: 'thing'});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call get method of storage to get a value', () => {
|
it('should call get method of storage to get a value', async () => {
|
||||||
spyOn(storage, 'get');
|
spyOn(storage, 'get').and.callFake(() => Promise.resolve());
|
||||||
storageProvider.get('some-uid');
|
try {
|
||||||
|
await storageProvider.get('some-uid');
|
||||||
|
}
|
||||||
|
// if not caught, causes issues and tests fail
|
||||||
|
catch {}
|
||||||
|
|
||||||
expect(storage.get).toHaveBeenCalledWith('some-uid');
|
expect(storage.get).toHaveBeenCalledWith('some-uid');
|
||||||
});
|
});
|
||||||
@@ -100,7 +107,8 @@ describe('StorageProvider', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should put multiple values into the storage', async () => {
|
it('should put multiple values into the storage', async () => {
|
||||||
spyOn(storageProvider, 'put');
|
// @ts-ignore
|
||||||
|
spyOn(storageProvider, 'put').and.callFake(() => Promise.resolve());
|
||||||
await storageProvider.putMultiple(sampleEntries);
|
await storageProvider.putMultiple(sampleEntries);
|
||||||
|
|
||||||
expect(storageProvider.put).toHaveBeenCalledTimes(sampleEntries.size);
|
expect(storageProvider.put).toHaveBeenCalledTimes(sampleEntries.size);
|
||||||
@@ -136,7 +144,7 @@ describe('StorageProvider', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should delete one or more entries from the storage', async () => {
|
it('should delete one or more entries from the storage', async () => {
|
||||||
const storageRemoveSpy = spyOn(storage, 'remove');
|
const storageRemoveSpy = spyOn(storage, 'remove').and.callFake(() => Promise.resolve());
|
||||||
|
|
||||||
await storageProvider.delete('bar');
|
await storageProvider.delete('bar');
|
||||||
expect(storage.remove).toHaveBeenCalledTimes(1);
|
expect(storage.remove).toHaveBeenCalledTimes(1);
|
||||||
@@ -148,7 +156,7 @@ describe('StorageProvider', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should delete all entries in the storage', async () => {
|
it('should delete all entries in the storage', async () => {
|
||||||
spyOn(storage, 'clear');
|
spyOn(storage, 'clear').and.callFake(() => Promise.resolve());
|
||||||
|
|
||||||
await storageProvider.deleteAll();
|
await storageProvider.deleteAll();
|
||||||
expect(storage.clear).toHaveBeenCalled();
|
expect(storage.clear).toHaveBeenCalled();
|
||||||
|
|||||||
Reference in New Issue
Block a user