forked from oliverschwendener/ueli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added test for WindowsApplicationRepository (oliverschwendener#…
- Loading branch information
1 parent
37057e1
commit c1fabb2
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/main/Extensions/ApplicationSearch/Windows/WindowsApplicationRepository.test.ts
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { Image } from "@common/Core/Image"; | ||
import type { AssetPathResolver } from "@Core/AssetPathResolver"; | ||
import type { FileImageGenerator } from "@Core/ImageGenerator"; | ||
import type { Logger } from "@Core/Logger"; | ||
import type { PowershellUtility } from "@Core/PowershellUtility"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import type { Settings } from "../Settings"; | ||
import { usePowershellScripts } from "./usePowershellScripts"; | ||
import { WindowsApplication } from "./WindowsApplication"; | ||
import { WindowsApplicationRepository } from "./WindowsApplicationRepository"; | ||
import type { WindowsApplicationRetrieverResult } from "./WindowsApplicationRetrieverResult"; | ||
import type { WindowsStoreApplication } from "./WindowsStoreApplication"; | ||
|
||
describe(WindowsApplicationRepository, () => { | ||
describe(WindowsApplicationRepository.prototype.getApplications, () => { | ||
const testGetApplications = async ({ | ||
expected, | ||
includeWindowsStoreApps, | ||
}: { | ||
expected: WindowsApplication[]; | ||
includeWindowsStoreApps: boolean; | ||
}) => { | ||
const folders = ["folder1", "folder2"]; | ||
const fileExtensions = ["ext1", "ext2"]; | ||
|
||
const executeScriptMock = vi.fn().mockImplementation((s) => { | ||
if (s === usePowershellScripts().getWindowsStoreApps) { | ||
return JSON.stringify(<WindowsStoreApplication[]>[ | ||
{ AppId: "1234", DisplayName: "WindowsStoreApp1", LogoBase64: "Logo1" }, | ||
{ AppId: "5678", DisplayName: "WindowsStoreApp2", LogoBase64: "Logo2" }, | ||
]); | ||
} else { | ||
return JSON.stringify(<WindowsApplicationRetrieverResult[]>[ | ||
{ BaseName: "App1", FullName: "PathToApp1" }, | ||
{ BaseName: "App2", FullName: "PathToApp2" }, | ||
]); | ||
} | ||
}); | ||
|
||
const powershellUtility = <PowershellUtility>{ executeScript: (s) => executeScriptMock(s) }; | ||
|
||
const settings = <Settings>{ | ||
getValue: (key) => { | ||
if (key === "includeWindowsStoreApps") { | ||
return includeWindowsStoreApps; | ||
} else if (key === "windowsFolders") { | ||
return folders; | ||
} else if (key === "windowsFileExtensions") { | ||
return fileExtensions; | ||
} | ||
}, | ||
}; | ||
|
||
const getImagesMock = vi.fn().mockResolvedValue({ PathToApp1: <Image>{ url: "PathToIcon1" } }); | ||
|
||
const fileImageGenerator = <FileImageGenerator>{ getImages: (f) => getImagesMock(f) }; | ||
|
||
const warnMock = vi.fn(); | ||
|
||
const logger = <Logger>{ warn: (m) => warnMock(m) }; | ||
|
||
const getExtensionAssetPathMock = vi.fn().mockReturnValue("path"); | ||
|
||
const assetPathResolver = <AssetPathResolver>{ | ||
getExtensionAssetPath: (e, a) => getExtensionAssetPathMock(e, a), | ||
}; | ||
|
||
const windowsApplicationRepository = new WindowsApplicationRepository( | ||
powershellUtility, | ||
settings, | ||
fileImageGenerator, | ||
logger, | ||
assetPathResolver, | ||
); | ||
|
||
expect(await windowsApplicationRepository.getApplications()).toEqual(expected); | ||
expect(executeScriptMock).toHaveBeenCalledTimes(includeWindowsStoreApps ? 2 : 1); | ||
expect(getImagesMock).toHaveBeenCalledWith(["PathToApp1", "PathToApp2"]); | ||
expect(getExtensionAssetPathMock).toHaveBeenCalledWith("ApplicationSearch", "windows-generic-app-icon.png"); | ||
expect(warnMock).toHaveBeenCalledWith( | ||
'Failed to generate icon for "PathToApp2". Using generic icon instead', | ||
); | ||
}; | ||
|
||
it("should return manually installed apps and windows store apps when 'include apps from windows store is enabled'", async () => | ||
await testGetApplications({ | ||
expected: [ | ||
new WindowsApplication("App1", "PathToApp1", { url: "PathToIcon1" }), | ||
new WindowsApplication("App2", "PathToApp2", { url: "file://path" }), | ||
new WindowsApplication("WindowsStoreApp1", "shell:AppsFolder\\1234", { | ||
url: "data:image/png;base64,Logo1", | ||
}), | ||
new WindowsApplication("WindowsStoreApp2", "shell:AppsFolder\\5678", { | ||
url: "data:image/png;base64,Logo2", | ||
}), | ||
], | ||
includeWindowsStoreApps: true, | ||
})); | ||
|
||
it("should return only manually installed apps and when 'include apps from windows store is disabled'", async () => | ||
await testGetApplications({ | ||
expected: [ | ||
new WindowsApplication("App1", "PathToApp1", { url: "PathToIcon1" }), | ||
new WindowsApplication("App2", "PathToApp2", { url: "file://path" }), | ||
], | ||
includeWindowsStoreApps: false, | ||
})); | ||
}); | ||
}); |