-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
useFetchNewUpdateInfosのテストを追加 (#1678)
* Refactor imports and add error handling for missing environment variables * window.electron.getAppInfosのモックは良くない * Fix issue with undefined latestVersion in HelpDialog.vue * Update useFetchNewUpdateInfos function description * Fix * タイポミス
- Loading branch information
Showing
6 changed files
with
146 additions
and
61 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
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
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,47 +1,57 @@ | ||
import { ref } from "vue"; | ||
import semver from "semver"; | ||
import { UpdateInfo } from "@/type/preload"; | ||
import { z } from "zod"; | ||
import { UpdateInfo, updateInfoSchema } from "@/type/preload"; | ||
|
||
// 最新版があるか調べる | ||
// 現バージョンより新しい最新版があれば`latestVersion`に代入される | ||
export const useFetchNewUpdateInfos = () => { | ||
const isCheckingFinished = ref<boolean>(false); | ||
const currentVersion = ref(""); | ||
const latestVersion = ref(""); | ||
const newUpdateInfos = ref<UpdateInfo[]>([]); | ||
/** | ||
* 現在のバージョンより新しいバージョンがリリースされているか調べる。 | ||
* あれば最新バージョンと、現在より新しいバージョンの情報を返す。 | ||
*/ | ||
export const useFetchNewUpdateInfos = ( | ||
currentVersionGetter: () => Promise<string>, | ||
newUpdateInfosUrl: string | ||
) => { | ||
const result = ref< | ||
| { | ||
status: "updateChecking"; | ||
} | ||
| { | ||
status: "updateAvailable"; | ||
latestVersion: string; | ||
newUpdateInfos: UpdateInfo[]; | ||
} | ||
| { | ||
status: "updateNotAvailable"; | ||
} | ||
>({ | ||
status: "updateChecking", | ||
}); | ||
|
||
window.electron | ||
.getAppInfos() | ||
.then((obj) => { | ||
currentVersion.value = obj.version; | ||
}) | ||
.then(() => { | ||
const url: string | undefined = import.meta.env | ||
.VITE_LATEST_UPDATE_INFOS_URL; | ||
if (!url) { | ||
throw new Error( | ||
"VITE_LATEST_UPDATE_INFOS_URLが未設定です。.env内に記載してください。" | ||
); | ||
(async () => { | ||
const currentVersion = await currentVersionGetter(); | ||
|
||
const updateInfos = await fetch(newUpdateInfosUrl).then( | ||
async (response) => { | ||
if (!response.ok) throw new Error("Network response was not ok."); | ||
return z.array(updateInfoSchema).parse(await response.json()); | ||
} | ||
fetch(url) | ||
.then((response) => { | ||
if (!response.ok) throw new Error("Network response was not ok."); | ||
return response.json(); | ||
}) | ||
.then((json) => { | ||
newUpdateInfos.value = json.filter((item: UpdateInfo) => { | ||
return semver.lt(currentVersion.value, item.version); | ||
}); | ||
if (newUpdateInfos.value?.length) { | ||
latestVersion.value = newUpdateInfos.value[0].version; | ||
} | ||
isCheckingFinished.value = true; | ||
}); | ||
); | ||
const newUpdateInfos = updateInfos.filter((item: UpdateInfo) => { | ||
return semver.lt(currentVersion, item.version); | ||
}); | ||
|
||
return { | ||
isCheckingFinished, | ||
latestVersion, | ||
newUpdateInfos, | ||
}; | ||
if (newUpdateInfos.length > 0) { | ||
result.value = { | ||
status: "updateAvailable", | ||
latestVersion: newUpdateInfos[0].version, | ||
newUpdateInfos, | ||
}; | ||
} else { | ||
result.value = { | ||
status: "updateNotAvailable", | ||
}; | ||
} | ||
})(); | ||
|
||
return result; | ||
}; |
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
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
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,56 @@ | ||
import { Ref } from "vue"; | ||
import { UpdateInfo } from "@/type/preload"; | ||
import { useFetchNewUpdateInfos } from "@/composables/useFetchNewUpdateInfos"; | ||
|
||
// 最新バージョンの情報をfetchするモックを作成する | ||
const setupFetchMock = (latestVersion: string) => { | ||
// fetchのモックを作成 | ||
const updateInfos: UpdateInfo[] = [ | ||
{ | ||
version: latestVersion, | ||
descriptions: [], | ||
contributors: [], | ||
}, | ||
]; | ||
vi.stubGlobal("fetch", async () => { | ||
return new Response(JSON.stringify(updateInfos), { status: 200 }); | ||
}); | ||
}; | ||
|
||
// 準備完了まで待機 | ||
const waitFinished = async (result: Ref<{ status: string }>) => { | ||
await vi.waitFor(() => { | ||
if (result.value.status === "updateChecking") throw new Error(); | ||
}); | ||
}; | ||
|
||
it("新バージョンがある場合、latestVersionに最新バージョンが代入される", async () => { | ||
const currentVersion = "1.0.0"; | ||
const latestVersion = "2.0.0"; | ||
setupFetchMock(latestVersion); | ||
|
||
const result = useFetchNewUpdateInfos( | ||
async () => currentVersion, | ||
"Dummy Url" | ||
); | ||
|
||
await waitFinished(result); | ||
expect(result.value).toMatchObject({ | ||
status: "updateAvailable", | ||
latestVersion, | ||
}); | ||
}); | ||
|
||
it("新バージョンがない場合は状態が変わるだけ", async () => { | ||
const currentVersion = "1.0.0"; | ||
const latestVersion = "1.0.0"; | ||
setupFetchMock(latestVersion); | ||
|
||
const result = useFetchNewUpdateInfos( | ||
async () => currentVersion, | ||
"Dummy Url" | ||
); | ||
|
||
await waitFinished(result); | ||
expect(result.value).toMatchObject({ status: "updateNotAvailable" }); | ||
}); |