From 579df18d95505d1238c3390bb88f6dbb99393d63 Mon Sep 17 00:00:00 2001 From: "Ruben S. Garcia" Date: Fri, 22 Dec 2023 21:14:00 +0100 Subject: [PATCH 1/5] chore: Remove interface export --- src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index 0d22123..7f585d2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -26,4 +26,4 @@ function configure(newConfig: Partial) { const getConfig = (): Config => ({ ...config }) -export { configure, getConfig, Config, mount } +export { configure, getConfig, mount } From fc419efa0c83c9cb0be6ea1fff6067253b4fa704 Mon Sep 17 00:00:00 2001 From: "Ruben S. Garcia" Date: Fri, 22 Dec 2023 21:14:12 +0100 Subject: [PATCH 2/5] feat: Add defaultResponses to Config --- src/models.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/models.ts b/src/models.ts index 8be1d04..a5b78a9 100644 --- a/src/models.ts +++ b/src/models.ts @@ -72,6 +72,7 @@ export interface Config { mount: Mount extend: Extensions changeRoute: (path: string) => void + defaultResponses?: Array history?: BrowserHistory portal?: string handleQueryParams?: boolean From 82e253f8771b38b21b4b79ba4c949d034cd0a29d Mon Sep 17 00:00:00 2001 From: "Ruben S. Garcia" Date: Fri, 22 Dec 2023 21:14:58 +0100 Subject: [PATCH 3/5] chore: Add component for testing --- tests/components.mock.jsx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/components.mock.jsx b/tests/components.mock.jsx index 08c38e6..dd33150 100644 --- a/tests/components.mock.jsx +++ b/tests/components.mock.jsx @@ -347,3 +347,35 @@ export const GreetingComponent = () => { return
Hi {name}!
} + +export const MyComponentWithFeatureFlags = () => { + const [flags, setFlags] = useState([]) + + useEffect(() => { + const retrieveFeatureFlags = async () => { + try { + const request = new Request('my-host/feature-flags') + const response = await fetch(request) + if (!response) return + const { flags } = await response.json() + + setFlags(flags) + } catch (e) { + setFlags([]) + } + } + + retrieveFeatureFlags() + }, []) + + return ( + <> +

Feature flags test

+ {flags.includes('my-flag') && ( +
+ Feature Flag enabled +
+ )} + + ) +} From f2c866ef3fc5e1aa2f5ca00fdd4d45fd61abc6c5 Mon Sep 17 00:00:00 2001 From: "Ruben S. Garcia" Date: Fri, 22 Dec 2023 21:15:18 +0100 Subject: [PATCH 4/5] feat: Add defaultResponse implementation --- src/wrap.tsx | 9 +++++-- tests/lib/withNetwork.test.ts | 48 ++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/wrap.tsx b/src/wrap.tsx index 778bf6d..2640b4d 100644 --- a/src/wrap.tsx +++ b/src/wrap.tsx @@ -113,7 +113,7 @@ const debugRequests = () => { } const getMount = () => { - const { portal, changeRoute, history, mount } = getConfig() + const { portal, changeRoute, history, mount, defaultResponses } = getConfig() const { Component, props, responses, path, hasPath, debug, historyState } = getOptions() @@ -137,7 +137,12 @@ const getMount = () => { changeRoute(path) } - mockNetwork(responses, debug) + let mockedResponses: Response[] = [...responses] + if (defaultResponses) { + mockedResponses = [...mockedResponses, ...defaultResponses] + } + + mockNetwork(mockedResponses, debug) return mount() } diff --git a/tests/lib/withNetwork.test.ts b/tests/lib/withNetwork.test.ts index 83ba512..35170f7 100644 --- a/tests/lib/withNetwork.test.ts +++ b/tests/lib/withNetwork.test.ts @@ -1,4 +1,3 @@ -import React from 'react' import { render, screen, fireEvent } from '@testing-library/react' import { wrap, configure } from '../../src/index' import { vi, it, expect } from 'vitest' @@ -8,6 +7,7 @@ import { MyComponentWithPost, MyComponentWithFeedback, MyComponentMakingHttpCallsWithQueryParams, + MyComponentWithFeatureFlags, } from '../components.mock' it('should have network by default', async () => { @@ -219,3 +219,49 @@ it('should handle fetch requests with option when a string is passed', async () expect(response).toEqual({ foo: 'bar' }) }) + +it('should be able to define global fetch mocks', async () => { + configure({ + mount: render, + defaultResponses: [ + { + path: 'my-host/feature-flags', + responseBody: { + flags: ['my-flag'], + }, + }, + ], + }) + + wrap(MyComponentWithFeatureFlags).mount() + + expect(await screen.findByText(/feature flag enabled/i)).toBeVisible() +}) + +it('should be able to override global fetch mocks', async () => { + configure({ + mount: render, + defaultResponses: [ + { + path: 'my-host/feature-flags', + responseBody: { + flags: ['my-flag'], + }, + }, + ], + }) + + wrap(MyComponentWithFeatureFlags) + .withNetwork([ + { + path: 'my-host/feature-flags', + responseBody: { + flags: ['another-flag'], + }, + }, + ]) + .mount() + + expect(await screen.findByText(/feature flags test/i)).toBeVisible() + expect(screen.queryByText(/feature flag enabled/i)).not.toBeInTheDocument() +}) From bf558b7284164551d934cf84b6f426fedc90bea6 Mon Sep 17 00:00:00 2001 From: "Ruben S. Garcia" Date: Fri, 22 Dec 2023 21:32:16 +0100 Subject: [PATCH 5/5] test: Works with extensions --- tests/lib/withNetwork.test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/lib/withNetwork.test.ts b/tests/lib/withNetwork.test.ts index 35170f7..222adc0 100644 --- a/tests/lib/withNetwork.test.ts +++ b/tests/lib/withNetwork.test.ts @@ -265,3 +265,35 @@ it('should be able to override global fetch mocks', async () => { expect(await screen.findByText(/feature flags test/i)).toBeVisible() expect(screen.queryByText(/feature flag enabled/i)).not.toBeInTheDocument() }) + +it('should work with extensions', async () => { + configure({ + mount: render, + defaultResponses: [ + { + path: 'my-host/feature-flags', + responseBody: { + flags: ['my-flag'], + }, + }, + ], + extend: { + withCustomFlags: ({ addResponses }, [otherResponses = []]) => { + addResponses([ + { + path: 'my-host/feature-flags', + responseBody: { + flags: ['another-flag'], + }, + }, + ...otherResponses, + ]) + }, + }, + }) + + wrap(MyComponentWithFeatureFlags).withCustomFlags().mount() + + expect(await screen.findByText(/feature flags test/i)).toBeVisible() + expect(screen.queryByText(/feature flag enabled/i)).not.toBeInTheDocument() +})