Skip to content

Commit 2411fbc

Browse files
varlamcgee
authored andcommitted
feat: rename MockProvider to CustomProvider and expose it (#8)
feat: rename MockProvider to CustomProvider and expose it
1 parent 71c2269 commit 2411fbc

File tree

7 files changed

+92
-81
lines changed

7 files changed

+92
-81
lines changed

runtime/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export {
2+
CustomProvider as CustomDataProvider,
23
Provider as DataProvider,
34
Query as DataQuery,
45
useQuery as useDataQuery,

services/data/src/__tests__/integration.test.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from 'react'
22
import { useQuery } from '..'
33
import { render, waitForElement } from 'react-testing-library'
4-
import { MockProvider } from '../components/MockProvider'
4+
import { CustomProvider } from '../components/CustomProvider'
55
import { Query } from '../components/Query'
66
import { QueryRenderInput } from '../types/Query'
77

8-
const mockData = {
8+
const customData = {
99
answer: 42,
1010
}
1111

12-
describe('Testing mock data provider and useQuery hook', () => {
12+
describe('Testing custom data provider and useQuery hook', () => {
1313
it('Should render without failing', async () => {
1414
const renderFunction = jest.fn(
1515
({ loading, error, data }: QueryRenderInput) => {
@@ -20,11 +20,11 @@ describe('Testing mock data provider and useQuery hook', () => {
2020
)
2121

2222
const { getByText } = render(
23-
<MockProvider mockData={mockData}>
23+
<CustomProvider data={customData}>
2424
<Query query={{ answer: { resource: 'answer' } }}>
2525
{renderFunction}
2626
</Query>
27-
</MockProvider>
27+
</CustomProvider>
2828
)
2929

3030
expect(getByText(/loading/i)).not.toBeUndefined()
@@ -36,15 +36,15 @@ describe('Testing mock data provider and useQuery hook', () => {
3636
expect(renderFunction).toHaveBeenCalledTimes(2)
3737
expect(renderFunction).toHaveBeenLastCalledWith({
3838
loading: false,
39-
data: mockData,
39+
data: customData,
4040
})
4141
expect(getByText(/data: /i)).toHaveTextContent(
42-
`data: ${mockData.answer}`
42+
`data: ${customData.answer}`
4343
)
4444
})
4545
})
4646

47-
describe('Testing mock data provider and useQuery hook', () => {
47+
describe('Testing custom data provider and useQuery hook', () => {
4848
it('Should render an error', async () => {
4949
const renderFunction = jest.fn(
5050
({ loading, error, data }: QueryRenderInput) => {
@@ -55,11 +55,11 @@ describe('Testing mock data provider and useQuery hook', () => {
5555
)
5656

5757
const { getByText } = render(
58-
<MockProvider mockData={mockData}>
58+
<CustomProvider data={customData}>
5959
<Query query={{ test: { resource: 'test' } }}>
6060
{renderFunction}
6161
</Query>
62-
</MockProvider>
62+
</CustomProvider>
6363
)
6464

6565
expect(getByText(/loading/i)).not.toBeUndefined()
@@ -70,10 +70,10 @@ describe('Testing mock data provider and useQuery hook', () => {
7070
await waitForElement(() => getByText(/error: /i))
7171
expect(renderFunction).toHaveBeenCalledTimes(2)
7272
expect(String(renderFunction.mock.calls[1][0].error)).toBe(
73-
'Error: No mock provided for resource type test!'
73+
'Error: No data provided for resource type test!'
7474
)
7575
// expect(getByText(/data: /i)).toHaveTextContent(
76-
// `data: ${mockData.answer}`
76+
// `data: ${customData.answer}`
7777
// )
7878
})
7979
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react'
2+
import {
3+
makeCustomContext,
4+
CustomContextData,
5+
CustomContextOptions,
6+
} from '../context/makeCustomContext'
7+
import { Context } from './Context'
8+
9+
interface CustomProviderInput {
10+
children: React.ReactNode
11+
data: CustomContextData
12+
options?: CustomContextOptions
13+
}
14+
export const CustomProvider = ({
15+
children,
16+
data,
17+
options,
18+
}: CustomProviderInput) => {
19+
const context = makeCustomContext(data, options)
20+
return <Context.Provider value={context}>{children}</Context.Provider>
21+
}

services/data/src/components/MockProvider.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,61 @@
1-
import { makeMockContext } from './makeMockContext'
1+
import { makeCustomContext } from './makeCustomContext'
22
import { QueryDefinition } from '../types/Query'
33
import { bigIntLiteral } from '@babel/types'
44

5-
describe('makeMockContext', () => {
6-
it('Should throw if requested resource is not mocked', () => {
7-
const context = makeMockContext({})
5+
describe('makeCustomContext', () => {
6+
it('Should throw if requested custom resource is not provided', () => {
7+
const context = makeCustomContext({})
88
expect(context.fetch({ resource: 'test' })).rejects.toBeTruthy()
99
})
1010
it('Should return empty object on miss if failOnMiss=false', async () => {
11-
const context = makeMockContext({}, { failOnMiss: false })
11+
const context = makeCustomContext({}, { failOnMiss: false })
1212
expect(await context.fetch({ resource: 'test' })).toEqual({})
1313
})
14-
it('Should return mocked resources', async () => {
15-
const mockData = {
14+
it('Should return custom resources', async () => {
15+
const customData = {
1616
test: { value: 'something' },
1717
user: { id: 42 },
1818
}
19-
const context = makeMockContext(mockData)
20-
expect(await context.fetch({ resource: 'test' })).toEqual(mockData.test)
21-
expect(await context.fetch({ resource: 'user' })).toEqual(mockData.user)
19+
const context = makeCustomContext(customData)
20+
expect(await context.fetch({ resource: 'test' })).toEqual(
21+
customData.test
22+
)
23+
expect(await context.fetch({ resource: 'user' })).toEqual(
24+
customData.user
25+
)
2226
})
23-
it('Should resolve mocked resource with function', async () => {
24-
const mockData = {
27+
it('Should resolve custom resource with function', async () => {
28+
const customData = {
2529
test: { value: 'something' },
2630
user: { id: 42 },
2731
}
28-
const context = makeMockContext(mockData)
29-
expect(await context.fetch({ resource: 'test' })).toEqual(mockData.test)
30-
expect(await context.fetch({ resource: 'user' })).toEqual(mockData.user)
32+
const context = makeCustomContext(customData)
33+
expect(await context.fetch({ resource: 'test' })).toEqual(
34+
customData.test
35+
)
36+
expect(await context.fetch({ resource: 'user' })).toEqual(
37+
customData.user
38+
)
3139
})
3240
it('Should throw if resolver returns undefined', async () => {
33-
const mockData = {
41+
const customData = {
3442
test: () => undefined,
3543
}
36-
const context = makeMockContext(mockData)
44+
const context = makeCustomContext(customData)
3745
expect(context.fetch({ resource: 'test' })).rejects.toBeTruthy()
3846
})
3947
it('Should not throw if resolver returns undefined with failOnMiss=false', async () => {
40-
const mockData = {
48+
const customData = {
4149
test: () => undefined,
4250
}
43-
const context = makeMockContext(mockData, { failOnMiss: false })
51+
const context = makeCustomContext(customData, { failOnMiss: false })
4452
expect(context.fetch({ resource: 'test' })).resolves.toEqual({})
4553
})
46-
it('Should throw if mock is something crazy like a bigint', async () => {
47-
const mockData = {
54+
it('Should throw if custom data is something crazy like a bigint', async () => {
55+
const customData = {
4856
test: null as any,
4957
}
50-
const context = makeMockContext(mockData, { failOnMiss: false })
58+
const context = makeCustomContext(customData, { failOnMiss: false })
5159
expect(context.fetch({ resource: 'test' })).resolves.toEqual({})
5260
})
5361
})

services/data/src/context/makeMockContext.ts renamed to services/data/src/context/makeCustomContext.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,76 @@ import { FetchErrorPayload } from '../types/FetchError'
77
const baseUrl = 'https://example.com'
88
const apiVersion = 42
99

10-
export type MockResourceLiteral =
10+
export type CustomResourceLiteral =
1111
| string
1212
| number
1313
| boolean
1414
| object
1515
| FetchErrorPayload
16-
export type MockResourceFactory = (
16+
export type CustomResourceFactory = (
1717
query: QueryDefinition
18-
) => Promise<MockResourceLiteral>
19-
export type MockResource = MockResourceLiteral | MockResourceFactory
20-
export interface MockContextData {
21-
[resourceName: string]: MockResource
18+
) => Promise<CustomResourceLiteral>
19+
export type CustomResource = CustomResourceLiteral | CustomResourceFactory
20+
export interface CustomContextData {
21+
[resourceName: string]: CustomResource
2222
}
23-
export type MockContextOptions = {
23+
export type CustomContextOptions = {
2424
failOnMiss?: boolean
2525
}
2626

27-
const resolveMock = async (
28-
mockResource: MockResource,
27+
const resolveCustomResource = async (
28+
customResource: CustomResource,
2929
query: QueryDefinition,
30-
{ failOnMiss }: MockContextOptions
31-
): Promise<MockResource> => {
32-
switch (typeof mockResource) {
30+
{ failOnMiss }: CustomContextOptions
31+
): Promise<CustomResource> => {
32+
switch (typeof customResource) {
3333
case 'string':
3434
case 'number':
3535
case 'boolean':
3636
case 'object':
37-
return mockResource
37+
return customResource
3838
case 'function':
3939
// function
40-
const result = await mockResource(query)
40+
const result = await customResource(query)
4141
if (!result && failOnMiss) {
4242
throw new Error(
43-
`The mock function for resource ${
43+
`The custom function for resource ${
4444
query.resource
4545
} must always return a value but returned ${result}`
4646
)
4747
}
4848
return result || {}
4949
default:
5050
// should be unreachable
51-
throw new Error(`Unknown mock type ${typeof mockResource}`)
51+
throw new Error(`Unknown resource type ${typeof customResource}`)
5252
}
5353
}
5454

55-
export const makeMockContext = (
56-
mockData: MockContextData,
57-
{ failOnMiss = true }: MockContextOptions = {}
55+
export const makeCustomContext = (
56+
customData: CustomContextData,
57+
{ failOnMiss = true }: CustomContextOptions = {}
5858
): ContextType => {
5959
const apiUrl = joinPath(baseUrl, 'api', String(apiVersion))
60-
const mockFetch: FetchFunction = async query => {
61-
const mockResource = mockData[query.resource]
62-
if (!mockResource) {
60+
const customFetch: FetchFunction = async query => {
61+
const customResource = customData[query.resource]
62+
if (!customResource) {
6363
if (failOnMiss) {
6464
throw new Error(
65-
`No mock provided for resource type ${query.resource}!`
65+
`No data provided for resource type ${query.resource}!`
6666
)
6767
}
6868
return Promise.resolve({})
6969
}
7070

71-
return await resolveMock(mockResource, query, { failOnMiss })
71+
return await resolveCustomResource(customResource, query, {
72+
failOnMiss,
73+
})
7274
}
7375
const context = {
7476
baseUrl,
7577
apiVersion,
7678
apiUrl,
77-
fetch: mockFetch,
79+
fetch: customFetch,
7880
}
7981
return context
8082
}

services/data/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { Query } from './components/Query'
22
export { Provider } from './components/Provider'
3-
export { MockProvider } from './components/MockProvider'
3+
export { CustomProvider } from './components/CustomProvider'
44
export { Context } from './components/Context'
55
export { useQuery } from './hooks/useQuery'

0 commit comments

Comments
 (0)