This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add missing test and fix failing one
- Loading branch information
1 parent
5857042
commit fdccd08
Showing
1 changed file
with
119 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,135 @@ | ||
import { SettingsService } from './settings.service' | ||
import { DBService } from '../../core/services/db.service'; | ||
import { DBStore } from '../../core/enums/db-stores.enum'; | ||
import { AppSetting } from '../../core/enums/app-settings.enum'; | ||
import { SettingsService } from "./settings.service"; | ||
import { DBService } from "../../core/services/db.service"; | ||
import { DBStore } from "../../core/enums/db-stores.enum"; | ||
import { AppSetting } from "../../core/enums/app-settings.enum"; | ||
import { UserService } from "../../core/services"; | ||
import { of, BehaviorSubject } from "rxjs"; | ||
import { async, TestBed } from "@angular/core/testing"; | ||
describe("SettingsService", () => { | ||
let settingsService: SettingsService; | ||
let dbServiceSpy: jasmine.SpyObj<DBService>; | ||
let settingsService: SettingsService; | ||
let mockUser = new BehaviorSubject({}); | ||
const dbServiceSpy = jasmine.createSpyObj<DBService>("DBService", [ | ||
"putEntry", | ||
"getEntry" | ||
]); | ||
|
||
beforeEach(() => { | ||
dbServiceSpy = jasmine.createSpyObj<DBService>('DBService', ['putEntry', 'getEntry']); | ||
settingsService = new SettingsService(dbServiceSpy); | ||
beforeEach(async(() => { | ||
const userService = { | ||
user: mockUser | ||
}; | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [ | ||
{ provide: UserService, useValue: userService }, | ||
{ provide: DBService, useValue: dbServiceSpy } | ||
] | ||
}); | ||
})); | ||
|
||
beforeEach(() => { | ||
settingsService = TestBed.get(SettingsService); | ||
}); | ||
|
||
afterEach(() => { | ||
dbServiceSpy.getEntry.calls.reset(); | ||
}); | ||
|
||
it('should exist', () => { | ||
expect(settingsService).toBeDefined(); | ||
it("should exist", () => { | ||
expect(settingsService).toBeDefined(); | ||
}); | ||
|
||
it("should pass settings to the DB service for persisting", () => { | ||
dbServiceSpy.putEntry.and.callFake(() => Promise.resolve(true)); | ||
settingsService.updateSetting(AppSetting.inaraAPIKey, "123456789abcde"); | ||
expect(dbServiceSpy.putEntry).toHaveBeenCalledWith( | ||
DBStore.appSettings, | ||
jasmine.objectContaining({ | ||
key: AppSetting.inaraAPIKey, | ||
value: "123456789abcde" | ||
}) | ||
); | ||
}); | ||
|
||
it("should fetch settings from the DB service", done => { | ||
dbServiceSpy.getEntry.and.callFake((store: string, entry: string) => { | ||
return Promise.resolve("123456789abcde"); | ||
}); | ||
|
||
it('should pass settings to the DB service for persisting', () => { | ||
dbServiceSpy.putEntry.and.callFake(() => Promise.resolve(true)); | ||
settingsService.updateSetting(AppSetting.inaraAPIKey, '123456789abcde'); | ||
expect(dbServiceSpy.putEntry) | ||
.toHaveBeenCalledWith( | ||
DBStore.appSettings, | ||
jasmine.objectContaining({ key: AppSetting.inaraAPIKey, value: '123456789abcde' }) | ||
) | ||
let setting = settingsService.getSetting(AppSetting.inaraAPIKey); | ||
setting.then(setting => { | ||
expect(setting).toEqual("123456789abcde"); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should fetch settings from the DB service', (done) => { | ||
dbServiceSpy.getEntry.and.callFake((store: string, entry: string) => { | ||
return Promise.resolve('123456789abcde'); | ||
}); | ||
it("should return cached settings if available", done => { | ||
dbServiceSpy.getEntry.and.callFake(() => Promise.resolve(undefined)); | ||
dbServiceSpy.putEntry.and.callFake(() => Promise.resolve(true)); | ||
|
||
let setting = settingsService.getSetting(AppSetting.inaraAPIKey); | ||
setting.then(setting => { | ||
expect(setting).toEqual('123456789abcde'); | ||
done(); | ||
settingsService.updateSetting(AppSetting.broadcasts, true).then(() => { | ||
settingsService | ||
.getSetting(AppSetting.broadcasts) | ||
.then((setting: { setting: AppSetting; value: any }) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).not.toHaveBeenCalled(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should return cached settings if available', (done) => { | ||
dbServiceSpy.getEntry.and.callFake(() => Promise.resolve(undefined)); | ||
dbServiceSpy.putEntry.and.callFake(() => Promise.resolve(true)); | ||
|
||
settingsService.updateSetting(AppSetting.broadcasts, true) | ||
.then(() => { | ||
settingsService.getSetting(AppSetting.broadcasts) | ||
.then((setting: {setting: AppSetting, value: any}) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).not.toHaveBeenCalled(); | ||
done(); | ||
}); | ||
}) | ||
}); | ||
it("should cache a setting if fetched from DB", done => { | ||
dbServiceSpy.getEntry.and.callFake(() => | ||
Promise.resolve({ setting: AppSetting.broadcasts, value: true }) | ||
); | ||
|
||
it('should cache a setting if fetched from DB', (done) => { | ||
dbServiceSpy.getEntry.and.callFake(() => Promise.resolve({setting: AppSetting.broadcasts, value: true})); | ||
|
||
settingsService.getSetting(AppSetting.broadcasts) | ||
.then((setting: {setting: AppSetting, value: any}) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).toHaveBeenCalled(); | ||
dbServiceSpy.getEntry.calls.reset(); | ||
}) | ||
.then(()=>settingsService.getSetting(AppSetting.broadcasts)) | ||
.then((setting: {setting: AppSetting, value: any})=>{ | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).not.toHaveBeenCalled(); | ||
done(); | ||
}) | ||
}); | ||
settingsService | ||
.getSetting(AppSetting.broadcasts) | ||
.then((setting: { setting: AppSetting; value: any }) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).toHaveBeenCalledTimes(1); | ||
}) | ||
.then(() => settingsService.getSetting(AppSetting.broadcasts)) | ||
.then((setting: { setting: AppSetting; value: any }) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).toHaveBeenCalledTimes(1); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should cache a setting when set", done => { | ||
dbServiceSpy.putEntry.and.callFake(() => Promise.resolve(true)); | ||
|
||
it('should cache a setting when set',(done)=>{ | ||
dbServiceSpy.putEntry.and.callFake(() => Promise.resolve(true)); | ||
|
||
settingsService.updateSetting(AppSetting.broadcasts, true) | ||
.then(() => { | ||
settingsService.getSetting(AppSetting.broadcasts) | ||
.then((setting: {setting: AppSetting, value: any}) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).not.toHaveBeenCalled(); | ||
done(); | ||
}); | ||
}) | ||
settingsService.updateSetting(AppSetting.broadcasts, true).then(() => { | ||
settingsService | ||
.getSetting(AppSetting.broadcasts) | ||
.then((setting: { setting: AppSetting; value: any }) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).not.toHaveBeenCalled(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it("should clear the settings cache when the user logs out", done => { | ||
dbServiceSpy.getEntry.and.callFake(() => | ||
Promise.resolve({ setting: AppSetting.broadcasts, value: true }) | ||
); | ||
|
||
}) | ||
settingsService | ||
.getSetting(AppSetting.broadcasts) | ||
.then((setting: { setting: AppSetting; value: any }) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).toHaveBeenCalledTimes(1); | ||
}) | ||
.then(() => { | ||
mockUser.next(null); | ||
mockUser.next({}); | ||
}) | ||
.then(() => settingsService.getSetting(AppSetting.broadcasts)) | ||
.then((setting: { setting: AppSetting; value: any }) => { | ||
expect(setting.value).toEqual(true); | ||
expect(dbServiceSpy.getEntry).toHaveBeenCalledTimes(2); | ||
done(); | ||
}); | ||
}); | ||
}); |