From 762feea33240ce58d9900041361d756f63af20cc Mon Sep 17 00:00:00 2001 From: flx-sta <50131232+flx-sta@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:17:34 -0700 Subject: [PATCH] [Bug][Localhost] fix Json parse issue when trying to load battle-anim (#3455) * add content-type check for loading battle-anims * add missing `headers` to response mocks --- src/data/battle-anims.ts | 3 ++- src/test/utils/gameWrapper.ts | 2 ++ src/test/utils/mocks/mockFetch.ts | 7 +++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/data/battle-anims.ts b/src/data/battle-anims.ts index bcf3bd7bb40..634c39b7d66 100644 --- a/src/data/battle-anims.ts +++ b/src/data/battle-anims.ts @@ -485,7 +485,8 @@ export function initMoveAnim(scene: BattleScene, move: Moves): Promise { const fetchAnimAndResolve = (move: Moves) => { scene.cachedFetch(`./battle-anims/${moveName}.json`) .then(response => { - if (!response.ok) { + const contentType = response.headers.get("content-type"); + if (!response.ok || contentType?.indexOf("application/json") === -1) { console.error(`Could not load animation file for move '${moveName}'`, response.status, response.statusText); populateMoveAnim(move, moveAnims.get(defaultMoveAnim)); return resolve(); diff --git a/src/test/utils/gameWrapper.ts b/src/test/utils/gameWrapper.ts index 7edb69aef8a..80529e47194 100644 --- a/src/test/utils/gameWrapper.ts +++ b/src/test/utils/gameWrapper.ts @@ -242,6 +242,7 @@ function createFetchResponse(data) { return { ok: true, status: 200, + headers: new Headers(), json: () => Promise.resolve(data), text: () => Promise.resolve(JSON.stringify(data)), }; @@ -251,6 +252,7 @@ function createFetchBadResponse(data) { return { ok: false, status: 404, + headers: new Headers(), json: () => Promise.resolve(data), text: () => Promise.resolve(JSON.stringify(data)), }; diff --git a/src/test/utils/mocks/mockFetch.ts b/src/test/utils/mocks/mockFetch.ts index ad364a95885..8043dd993fe 100644 --- a/src/test/utils/mocks/mockFetch.ts +++ b/src/test/utils/mocks/mockFetch.ts @@ -23,10 +23,13 @@ export const MockFetch = (input, init) => { } } - return Promise.resolve({ + const response: Partial = { ok: true, status: 200, json: responseHandler, text: responseText, - }); + headers: new Headers({}), + }; + + return Promise.resolve(response); };