|
| 1 | +import { Yok } from "../../lib/common/yok"; |
| 2 | +import { UserSettingsService } from "../../lib/services/user-settings-service"; |
| 3 | +import { assert } from "chai"; |
| 4 | +import * as path from "path"; |
| 5 | + |
| 6 | +class JsonFileSettingsServiceMock { |
| 7 | + constructor(public jsonFileSettingsPath: string) { } |
| 8 | +} |
| 9 | + |
| 10 | +describe("userSettingsService", () => { |
| 11 | + const profileDir = "my-profile-dir"; |
| 12 | + const expectedJsonFileSettingsFilePath = path.join(profileDir, "user-settings.json"); |
| 13 | + |
| 14 | + const createTestInjector = (): IInjector => { |
| 15 | + const testInjector = new Yok(); |
| 16 | + testInjector.register("settingsService", { |
| 17 | + getProfileDir: () => profileDir |
| 18 | + }); |
| 19 | + |
| 20 | + testInjector.register("jsonFileSettingsService", JsonFileSettingsServiceMock); |
| 21 | + testInjector.register("userSettingsService", UserSettingsService); |
| 22 | + return testInjector; |
| 23 | + }; |
| 24 | + |
| 25 | + const testCases = [ |
| 26 | + { |
| 27 | + methodName: "getSettingValue", |
| 28 | + input: ["settingName"], |
| 29 | + expectedArgs: [ |
| 30 | + "settingName", |
| 31 | + undefined |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + methodName: "saveSetting", |
| 36 | + input: ["settingName", "settingValue"], |
| 37 | + expectedArgs: [ |
| 38 | + "settingName", |
| 39 | + "settingValue", |
| 40 | + undefined |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + methodName: "saveSettings", |
| 45 | + input: [{ value: { subValue: 1 } }], |
| 46 | + expectedArgs: [ |
| 47 | + { value: { subValue: 1 } }, |
| 48 | + undefined |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + methodName: "removeSetting", |
| 53 | + input: ["settingName"], |
| 54 | + expectedArgs: [ |
| 55 | + "settingName" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + methodName: "loadUserSettingsFile", |
| 60 | + input: [], |
| 61 | + expectedArgs: [] |
| 62 | + } |
| 63 | + ]; |
| 64 | + |
| 65 | + for (const testCase of testCases) { |
| 66 | + it(`calls ${testCase.methodName} method of jsonFileSettingsService with correct args`, async () => { |
| 67 | + const testInjector = createTestInjector(); |
| 68 | + const dataPassedToJsonFileSettingsService: any[] = []; |
| 69 | + const userSettingsService = testInjector.resolve("userSettingsService"); |
| 70 | + const jsonFileSettingsService = userSettingsService.$jsonFileSettingsService; |
| 71 | + |
| 72 | + jsonFileSettingsService[testCase.methodName] = async (...args: any[]): Promise<void> => { |
| 73 | + dataPassedToJsonFileSettingsService.push(...args); |
| 74 | + }; |
| 75 | + |
| 76 | + await userSettingsService[testCase.methodName](...testCase.input); |
| 77 | + |
| 78 | + assert.deepEqual(dataPassedToJsonFileSettingsService, testCase.expectedArgs); |
| 79 | + assert.equal(jsonFileSettingsService.jsonFileSettingsPath, expectedJsonFileSettingsFilePath); |
| 80 | + }); |
| 81 | + } |
| 82 | +}); |
0 commit comments