From 235beab340692dc5a5c5930440ed84489bf8fe55 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Thu, 16 Jan 2025 12:16:44 +0100 Subject: [PATCH] fix: Wrap cozy-dataproxy-lib imports in a mockable file For some reasons, the ReactNative API fails to resolve `cozy-dataproxy-lib/api` path so we must use `/dist` version instead On the opposite, Jest will fail to resolve the `/dist` version so we cannot mock cozy-dataproxy-lib To fix this, we wrap the imports in a local file so we can mock this local file in Jest --- __tests__/jestSetupFile.js | 4 ++++ src/app/domain/search/dataproxy-wrapper.ts | 5 +++++ src/app/domain/search/search.ts | 5 ++--- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 src/app/domain/search/dataproxy-wrapper.ts diff --git a/__tests__/jestSetupFile.js b/__tests__/jestSetupFile.js index 56cbf5c54..8e0939f08 100644 --- a/__tests__/jestSetupFile.js +++ b/__tests__/jestSetupFile.js @@ -146,3 +146,7 @@ jest.mock('cozy-pouch-link', () => { jest.mock('react-native-mail', () => ({ mail: jest.fn() })) + +jest.mock('/app/domain/search/dataproxy-wrapper', () => ({ + SearchEngine: jest.fn() +})) diff --git a/src/app/domain/search/dataproxy-wrapper.ts b/src/app/domain/search/dataproxy-wrapper.ts new file mode 100644 index 000000000..63cd4434e --- /dev/null +++ b/src/app/domain/search/dataproxy-wrapper.ts @@ -0,0 +1,5 @@ +// We cannot use `cozy-dataproxy-lib/api` here due to our bundler configuration +// even if it is declared in the package.json file. Instead we should target the +// 'dist' directory. +// In the future we should find a way to homogenize bundlers configuration. +export { SearchEngine } from 'cozy-dataproxy-lib/dist/api' diff --git a/src/app/domain/search/search.ts b/src/app/domain/search/search.ts index d85aa00d7..86df5d9ac 100644 --- a/src/app/domain/search/search.ts +++ b/src/app/domain/search/search.ts @@ -1,10 +1,9 @@ import CozyClient from 'cozy-client' -// We cannot use `cozy-dataproxy-lib/api` here due to our bundler configuration. -// In the future we should find a way to homogenize bundlers configuration. -import { SearchEngine } from 'cozy-dataproxy-lib/dist/api' import type { SearchOptions } from 'cozy-dataproxy-lib/dist/search/types' import Minilog from 'cozy-minilog' +import { SearchEngine } from '/app/domain/search/dataproxy-wrapper' + const log = Minilog('📱🗂️ Search') let searchEngine: SearchEngine | undefined = undefined