Skip to content

Commit

Permalink
task(prettier): run prettier with updated rules
Browse files Browse the repository at this point in the history
  • Loading branch information
WillPoulson committed Aug 3, 2021
1 parent 9a23f8e commit 10e5c37
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 129 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Ionic cache service

Ionic cache service that can cache almost everything. **It caches requests, observables, promises and classic data.** It uses [Ionic Storage](https://ionicframework.com/docs/storage/) so we support IndexedDB, SQLite (Cordova), WebSQL in this order.
It can be used separatelety in Angular 2 application.

Expand Down Expand Up @@ -364,4 +365,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const config = {
}
},
moduleNameMapper: {
'^@ionic/storage':
'<rootDir>/node_modules/@ionic/storage/dist/esm/index.d.ts'
'^@ionic/storage': '<rootDir>/node_modules/@ionic/storage/dist/esm/index.d.ts'
}
};

Expand Down
16 changes: 7 additions & 9 deletions projects/ionic-cache/src/lib/cache.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ export const CONFIG = new InjectionToken<CacheConfig>('CONFIG');
imports: [
IonicStorageModule.forRoot({
name: '__ionicCache',
driverOrder: ['indexeddb', 'sqlite', 'websql'],
}),
],
driverOrder: ['indexeddb', 'sqlite', 'websql']
})
]
})
export class CacheModule {
static forRoot(
cacheConfig?: CacheConfig
): ModuleWithProviders<CacheModule> {
static forRoot(cacheConfig?: CacheConfig): ModuleWithProviders<CacheModule> {
return {
ngModule: CacheModule,
providers: [
{
provide: CONFIG,
useValue: { ...defaultConfig, ...cacheConfig },
useValue: { ...defaultConfig, ...cacheConfig }
},
CacheService,
],
CacheService
]
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CacheConfig } from '../interfaces/cache-config.interface';

export const defaultConfig: CacheConfig = {
keyPrefix: '',
keyPrefix: ''
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export const errorMessages = {
notEnabled: 'Cache is not enabled.',
expired: 'Cache entry already expired: ',
notFound: 'No such key: ',
browserOffline: 'No entries were deleted, because browser is offline.',
browserOffline: 'No entries were deleted, because browser is offline.'
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function decodeRawData(data: StorageCacheItem): Promise<any> {
status: dataJson.status,
headers: dataJson.headers,
statusText: dataJson.statusText,
url: dataJson.url,
url: dataJson.url
};

return new HttpResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export class CacheStorageService {
* Returns whether or not an object is a cached item.
*/
private isCachedItem(key: string, item: any): boolean {
return (
item && item.expires && item.type && key.startsWith(this.keyPrefix)
);
return item && item.expires && item.type && key.startsWith(this.keyPrefix);
}

/**
Expand Down
98 changes: 30 additions & 68 deletions projects/ionic-cache/src/lib/services/cache/cache.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ describe('CacheService', () => {
CacheService,
{
provide: CacheStorageService,
useValue: dependencies.cacheStorageService,
},
],
useValue: dependencies.cacheStorageService
}
]
});
service = TestBed.inject(CacheService);
});
Expand Down Expand Up @@ -124,13 +124,11 @@ describe('CacheService', () => {
it('should save the json string to storage', async () => {
const mockDataJson = JSON.stringify(mockData);

expect(
dependencies.cacheStorageService.set
).toHaveBeenCalledWith(mockKey, {
expect(dependencies.cacheStorageService.set).toHaveBeenCalledWith(mockKey, {
value: mockDataJson,
expires: expect.any(Number),
type: 'object',
groupKey: 'none',
groupKey: 'none'
});
});
});
Expand All @@ -142,21 +140,19 @@ describe('CacheService', () => {
statusText: 'Success',
headers: ['header'],
url: 'https://google.com',
body: {},
body: {}
};
return service.saveItem(mockKey, mockData);
});

it('should save the json string to storage with the type of response', async () => {
const mockDataJson = JSON.stringify(mockData);

expect(
dependencies.cacheStorageService.set
).toHaveBeenCalledWith(mockKey, {
expect(dependencies.cacheStorageService.set).toHaveBeenCalledWith(mockKey, {
value: mockDataJson,
expires: expect.any(Number),
type: 'response',
groupKey: 'none',
groupKey: 'none'
});
});
});
Expand All @@ -171,13 +167,11 @@ describe('CacheService', () => {
const mockDataBase64 = await convertBlobToBase64(mockData);
const mockDataJson = JSON.stringify(mockDataBase64);

expect(
dependencies.cacheStorageService.set
).toHaveBeenCalledWith(mockKey, {
expect(dependencies.cacheStorageService.set).toHaveBeenCalledWith(mockKey, {
value: mockDataJson,
expires: expect.any(Number),
type: 'text/html',
groupKey: 'none',
groupKey: 'none'
});
});
});
Expand Down Expand Up @@ -207,9 +201,7 @@ describe('CacheService', () => {
});

it('should remove the item in storage', () => {
expect(
dependencies.cacheStorageService.remove
).toHaveBeenCalledWith(mockKey);
expect(dependencies.cacheStorageService.remove).toHaveBeenCalledWith(mockKey);
});
});

Expand All @@ -236,13 +228,11 @@ describe('CacheService', () => {
{ key: 'songs/metal/1', data: 'Who Bit the Moon' },
{ key: 'songs/metal/2', data: 'Hail The Apocalypse' },
{ key: 'songs/electronica/1', data: 'Power Glove' },
{ key: 'songs/electronica/2', data: 'Centipede' },
{ key: 'songs/electronica/2', data: 'Centipede' }
];

beforeEach(() => {
dependencies.cacheStorageService.all.mockReturnValue(
Promise.resolve(mockStorageItems as any)
);
dependencies.cacheStorageService.all.mockReturnValue(Promise.resolve(mockStorageItems as any));
});

describe('when using a wildcard', () => {
Expand All @@ -251,9 +241,7 @@ describe('CacheService', () => {
});

it('should remove songs/electronica/2', () => {
expect(
dependencies.cacheStorageService.remove
).toHaveBeenCalledWith('songs/electronica/2');
expect(dependencies.cacheStorageService.remove).toHaveBeenCalledWith('songs/electronica/2');
});
});
});
Expand All @@ -279,16 +267,12 @@ describe('CacheService', () => {
const mockKey = 'key';

beforeEach(() => {
dependencies.cacheStorageService.get.mockReturnValue(
Promise.resolve({})
);
dependencies.cacheStorageService.get.mockReturnValue(Promise.resolve({}));
return service.getRawItem(mockKey);
});

it('should get the data from storage', () => {
expect(
dependencies.cacheStorageService.get
).toHaveBeenCalledWith(mockKey);
expect(dependencies.cacheStorageService.get).toHaveBeenCalledWith(mockKey);
});
});

Expand Down Expand Up @@ -337,9 +321,7 @@ describe('CacheService', () => {
});

it('should check if the key exists in storage', () => {
expect(
dependencies.cacheStorageService.exists
).toHaveBeenCalledWith(mockKey);
expect(dependencies.cacheStorageService.exists).toHaveBeenCalledWith(mockKey);
});
});

Expand Down Expand Up @@ -367,40 +349,32 @@ describe('CacheService', () => {
beforeEach(() => {
mockData = {
value: JSON.stringify({ example: 'test' }),
expires: new Date().getTime() + 10000,
expires: new Date().getTime() + 10000
};
dependencies.cacheStorageService.get.mockReturnValue(
Promise.resolve(mockData)
);
dependencies.cacheStorageService.get.mockReturnValue(Promise.resolve(mockData));
return service.getItem(mockKey);
});

it('should get if the key from storage', () => {
expect(
dependencies.cacheStorageService.get
).toHaveBeenCalledWith(mockKey);
expect(dependencies.cacheStorageService.get).toHaveBeenCalledWith(mockKey);
});
});

describe('when the data has expired', () => {
beforeEach(() => {
mockData = {
value: JSON.stringify({ example: 'test' }),
expires: new Date().getTime() - 10000,
expires: new Date().getTime() - 10000
};
dependencies.cacheStorageService.get.mockReturnValue(
Promise.resolve(mockData)
);
dependencies.cacheStorageService.get.mockReturnValue(Promise.resolve(mockData));
});

describe('when invalidateOffline is true and online', () => {
it('should return an error', async () => {
try {
await service.getItem(mockKey);
} catch (error) {
expect(error.message).toBe(
errorMessages.expired + mockKey
);
expect(error.message).toBe(errorMessages.expired + mockKey);
}
});
});
Expand All @@ -413,9 +387,7 @@ describe('CacheService', () => {
});

it('should get if the key from storage', () => {
expect(
dependencies.cacheStorageService.get
).toHaveBeenCalledWith(mockKey);
expect(dependencies.cacheStorageService.get).toHaveBeenCalledWith(mockKey);
});
});
});
Expand Down Expand Up @@ -445,38 +417,28 @@ describe('CacheService', () => {

describe('when the item exists', () => {
beforeEach(() => {
dependencies.cacheStorageService.get.mockReturnValue(
Promise.resolve({})
);
dependencies.cacheStorageService.get.mockReturnValue(Promise.resolve({}));
return service.getOrSetItem(mockKey, () => Promise.resolve({}));
});

it('should get the item from storage', () => {
expect(
dependencies.cacheStorageService.get
).toHaveBeenCalledWith(mockKey);
expect(dependencies.cacheStorageService.get).toHaveBeenCalledWith(mockKey);
});
});

describe('when the item does not exist', () => {
const mockData = { data: true };
beforeEach(() => {
dependencies.cacheStorageService.get.mockReturnValue(
Promise.reject()
);
return service.getOrSetItem(mockKey, () =>
Promise.resolve(mockData)
);
dependencies.cacheStorageService.get.mockReturnValue(Promise.reject());
return service.getOrSetItem(mockKey, () => Promise.resolve(mockData));
});

it('should set the item in storage', () => {
expect(
dependencies.cacheStorageService.set
).toHaveBeenCalledWith(mockKey, {
expect(dependencies.cacheStorageService.set).toHaveBeenCalledWith(mockKey, {
value: JSON.stringify(mockData),
expires: expect.any(Number),
type: 'object',
groupKey: 'none',
groupKey: 'none'
});
});
});
Expand Down
Loading

0 comments on commit 10e5c37

Please sign in to comment.