From 25867c34682dd3969e834de94bdd0a7473ea4a3b Mon Sep 17 00:00:00 2001 From: Drew Volz Date: Wed, 18 Oct 2023 20:17:08 -0600 Subject: [PATCH] add test for app-config useCourseSearchRecentsScreen - add @testing-library/react-native - test out prod enabled vs dev enabled for app-config flag --- .../useCourseSearchRecentsScreen.test.tsx | 63 +++++++++++ package-lock.json | 102 ++++++++++++++++++ package.json | 1 + 3 files changed, 166 insertions(+) create mode 100644 modules/app-config/__tests__/useCourseSearchRecentsScreen.test.tsx diff --git a/modules/app-config/__tests__/useCourseSearchRecentsScreen.test.tsx b/modules/app-config/__tests__/useCourseSearchRecentsScreen.test.tsx new file mode 100644 index 0000000000..00125e8718 --- /dev/null +++ b/modules/app-config/__tests__/useCourseSearchRecentsScreen.test.tsx @@ -0,0 +1,63 @@ +import React, {ReactElement} from 'react' +import {describe, expect, test} from '@jest/globals' +import {renderHook, waitFor} from '@testing-library/react-native' +import {QueryClient, QueryClientProvider} from '@tanstack/react-query' +import {useCourseSearchRecentsScreen} from '../index' + +jest.mock('../../../source/lib/storage', () => ({ + getFeatureFlag: jest.fn(), +})) + +jest.mock('@frogpond/constants', () => ({ + isDevMode: jest.fn(), +})) + +describe('useCourseSearchRecentsScreen', () => { + let queryClient: QueryClient + + beforeAll(() => { + queryClient = new QueryClient() + }) + + afterAll(() => { + queryClient.clear() + queryClient.removeQueries() + }) + + const queryWrapper = ({children}: {children: ReactElement}) => ( + {children} + ) + + // eslint-disable-next-line @typescript-eslint/no-var-requires + const isDevModeMock = require('@frogpond/constants').isDevMode + + const getFeatureFlagMock = + // eslint-disable-next-line @typescript-eslint/no-var-requires + require('../../../source/lib/storage').getFeatureFlag + + test('it should return true in dev when feature is enabled', async () => { + isDevModeMock.mockReturnValue(true) + getFeatureFlagMock.mockReturnValue(true) + + const {result} = renderHook(() => useCourseSearchRecentsScreen(), { + wrapper: queryWrapper, + }) + + await waitFor(() => { + expect(result.current).toBe(true) + }) + }) + + test('it should return false in prod when feature is enabled', async () => { + isDevModeMock.mockReturnValue(false) + getFeatureFlagMock.mockReturnValue(true) + + const {result} = renderHook(() => useCourseSearchRecentsScreen(), { + wrapper: queryWrapper, + }) + + await waitFor(() => { + expect(result.current).toBe(false) + }) + }) +}) diff --git a/package-lock.json b/package-lock.json index a8fd227abf..4bf8092762 100644 --- a/package-lock.json +++ b/package-lock.json @@ -77,6 +77,7 @@ "@jest/globals": "29.6.4", "@sentry/cli": "2.20.6", "@tanstack/eslint-plugin-query": "4.34.1", + "@testing-library/react-native": "12.3.0", "@types/base-64": "1.0.0", "@types/http-cache-semantics": "4.0.1", "@types/jest": "29.5.4", @@ -6051,6 +6052,28 @@ "@tanstack/react-query": "^4.33.0" } }, + "node_modules/@testing-library/react-native": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@testing-library/react-native/-/react-native-12.3.0.tgz", + "integrity": "sha512-E2Prl8D/ey9sRgzLyfCPPmqvsevJW5tVUy4E/pBbO7nOP6OzI/jn2P3vctn/kFdkPY2aE/udlU5ZeNAben9U2A==", + "dev": true, + "dependencies": { + "jest-matcher-utils": "^29.6.2", + "pretty-format": "^29.6.2", + "redent": "^3.0.0" + }, + "peerDependencies": { + "jest": ">=28.0.0", + "react": ">=16.8.0", + "react-native": ">=0.59", + "react-test-renderer": ">=16.8.0" + }, + "peerDependenciesMeta": { + "jest": { + "optional": true + } + } + }, "node_modules/@tsconfig/node10": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", @@ -15433,6 +15456,15 @@ "node": ">=6" } }, + "node_modules/min-indent": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", + "integrity": "sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -16840,6 +16872,20 @@ "ws": "^7" } }, + "node_modules/react-dom": { + "version": "18.2.0", + "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz", + "integrity": "sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==", + "optional": true, + "peer": true, + "dependencies": { + "loose-envify": "^1.1.0", + "scheduler": "^0.23.0" + }, + "peerDependencies": { + "react": "^18.2.0" + } + }, "node_modules/react-freeze": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/react-freeze/-/react-freeze-1.0.3.tgz", @@ -17417,6 +17463,28 @@ "react": "^16.0.0 || ^17.0.0 || ^18.0.0" } }, + "node_modules/react-test-renderer": { + "version": "18.2.0", + "resolved": "https://registry.npmjs.org/react-test-renderer/-/react-test-renderer-18.2.0.tgz", + "integrity": "sha512-JWD+aQ0lh2gvh4NM3bBM42Kx+XybOxCpgYK7F8ugAlpaTSnWsX+39Z4XkOykGZAHrjwwTZT3x3KxswVWxHPUqA==", + "dev": true, + "peer": true, + "dependencies": { + "react-is": "^18.2.0", + "react-shallow-renderer": "^16.15.0", + "scheduler": "^0.23.0" + }, + "peerDependencies": { + "react": "^18.2.0" + } + }, + "node_modules/react-test-renderer/node_modules/react-is": { + "version": "18.2.0", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz", + "integrity": "sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==", + "dev": true, + "peer": true + }, "node_modules/readable-stream": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", @@ -17455,6 +17523,28 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" }, + "node_modules/redent": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redent/-/redent-3.0.0.tgz", + "integrity": "sha512-6tDA8g98We0zd0GvVeMT9arEOnTw9qM03L9cJXaCjrip1OO764RDBLBfrB4cwzNGDj5OA5ioymC9GkizgWJDUg==", + "dev": true, + "dependencies": { + "indent-string": "^4.0.0", + "strip-indent": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/redent/node_modules/indent-string": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-4.0.0.tgz", + "integrity": "sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/redux": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/redux/-/redux-4.2.1.tgz", @@ -18736,6 +18826,18 @@ "node": ">=6" } }, + "node_modules/strip-indent": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-indent/-/strip-indent-3.0.0.tgz", + "integrity": "sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ==", + "dev": true, + "dependencies": { + "min-indent": "^1.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", diff --git a/package.json b/package.json index 3fc1e26ca1..a566a44328 100644 --- a/package.json +++ b/package.json @@ -113,6 +113,7 @@ "@jest/globals": "29.6.4", "@sentry/cli": "2.20.6", "@tanstack/eslint-plugin-query": "4.34.1", + "@testing-library/react-native": "12.3.0", "@types/base-64": "1.0.0", "@types/http-cache-semantics": "4.0.1", "@types/jest": "29.5.4",