Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add globalResponses configuration #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ function configure(newConfig: Partial<Config>) {

const getConfig = (): Config => ({ ...config })

export { configure, getConfig, Config, mount }
export { configure, getConfig, mount }
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface Config {
mount: Mount
extend: Extensions
changeRoute: (path: string) => void
defaultResponses?: Array<WrapResponse>
history?: BrowserHistory
portal?: string
handleQueryParams?: boolean
Expand Down
9 changes: 7 additions & 2 deletions src/wrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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(<C {...props} />)
}
Expand Down
32 changes: 32 additions & 0 deletions tests/components.mock.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,35 @@ export const GreetingComponent = () => {

return <div>Hi {name}!</div>
}

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 (
<>
<h1>Feature flags test</h1>
{flags.includes('my-flag') && (
<div>
<span>Feature Flag enabled</span>
</div>
)}
</>
)
}
80 changes: 79 additions & 1 deletion tests/lib/withNetwork.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -8,6 +7,7 @@ import {
MyComponentWithPost,
MyComponentWithFeedback,
MyComponentMakingHttpCallsWithQueryParams,
MyComponentWithFeatureFlags,
} from '../components.mock'

it('should have network by default', async () => {
Expand Down Expand Up @@ -219,3 +219,81 @@ 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()
})

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()
})