Skip to content

Commit

Permalink
feat: Consumed npm version and tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Ginnivan committed Aug 30, 2019
1 parent e2fbbf3 commit 58122e6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 38 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug test: Current file",
"program": "${workspaceRoot}/node_modules/.bin/jest",
"args": ["${relativeFile}", "--runInBand"],
"cwd": "${workspaceRoot}",
"sourceMaps": true
}
]
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build:esm": "tsc -p ./tsconfig.build.json --outDir ./dist/esm --module es2015",
"build": "yarn build:commonjs && yarn build:esm",
"lint": "eslint --ext .js,.ts .",
"test": "jest"
"test": "jest",
"verify": "yarn build && yarn test && yarn lint"
},
"jest": {
"transform": {
Expand All @@ -25,7 +26,7 @@
"devDependencies": {
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^24.0.18",
"@types/node": "^10.14.16",
"@types/node": "^12.7.3",
"@types/react": "^16.9.2",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
Expand All @@ -42,12 +43,13 @@
"react-ssr-data-loader": "^1.1.0",
"ts-jest": "^24.0.2",
"tslib": "^1.10.0",
"typescript": "^3.5.3"
"typescript": "3.5.3"
},
"dependencies": {
"typescript-log": "^1.1.0"
"json-react-layouts": "^1.0.3",
"typescript-log": "^1.1.1"
},
"peerDependencies": {
"react-ssr-data-loader": "^1.1.0"
}
}
}
8 changes: 4 additions & 4 deletions src/DataLoading.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentRegistrar } from 'react-json-page-layout'
import { LayoutApi } from 'json-react-layouts'

export interface DataDefinition<TConfig extends {}, TData, LoadDataServices> {
getCacheKey?: (config: TConfig, services: LoadDataServices) => string
Expand All @@ -10,9 +10,9 @@ export interface ComponentState<TData> {
data: MaybeLoaded<TData>
}

export interface LoadArguments<LoadDataServices> {
export interface LoadArguments<Services> {
componentRenderPath: string
dataDefinition: DataDefinition<any, any, LoadDataServices>
dataDefinition: DataDefinition<any, any, Services>
dataDefinitionArgs: any
componentRegistrar: ComponentRegistrar<LoadDataServices, any>
layout: LayoutApi<any, any, Services, any>
}
18 changes: 18 additions & 0 deletions src/__snapshots__/data-loading.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`can load data for component 1`] = `
Object {
"componentRenderPath": "test[0]",
"componentType": "test-with-data",
"dataProps": Object {
"data": Object {
"dataDefinitionArgs": Object {
"dataArg": "Foo",
},
"loaded": true,
"result": 3,
},
},
"length": 3,
}
`;
33 changes: 20 additions & 13 deletions src/data-loading.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import React from 'react'
import Adapter from 'enzyme-adapter-react-16'
import { init } from '.'
import { DataDefinition } from './DataLoading'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { DataLoaderResources, DataProvider } from 'react-ssr-data-loader'
import { getRegistrationCreators, ComponentRegistrar, CompositionRegistrar, RouteBuilder } from 'react-json-page-layout'
import { getRegistrationCreators, LayoutRegistration } from 'json-react-layouts'
import { mount, configure } from 'enzyme'

configure({ adapter: new Adapter() })

const lengthCalculatorDataDefinition: DataDefinition<{ dataArg: string }, number, {}> = {
loadData: props => new Promise(resolve => setTimeout(() => resolve(props.dataArg.length))),
loadData: props =>
new Promise(resolve =>
setTimeout(() => {
resolve(props.dataArg.length)
}),
),
}

// Test component with data
Expand Down Expand Up @@ -41,30 +47,31 @@ export const testComponentWithDataRegistration = createRegisterableComponentWith
)

it('can load data for component', async () => {
const componentRegistrar = new ComponentRegistrar()
.register(testComponentWithDataRegistration)
.registerMiddleware(middleware)
const compositionRegisrar = CompositionRegistrar.create(componentRegistrar).registerComposition(
testCompositionRegistration,
)
const routeBuilder = new RouteBuilder(compositionRegisrar)
const layout = new LayoutRegistration()
.registerComponents(registrar =>
registrar
.registerComponent(testComponentWithDataRegistration)
.registerMiddleware(middleware),
)
.registerCompositions(registrar =>
registrar.registerComposition(testCompositionRegistration),
)

const wrapper = mount(
<DataProvider resources={resources} globalProps={{}}>
<compositionRegisrar.ContentAreaRenderer
<layout.ContentAreaRenderer
componentRenderPath="test"
contentArea={[
{ type: 'test-with-data', props: { dataDefinitionArgs: { dataArg: 'Foo' } } },
]}
routeBuilder={routeBuilder}
loadDataServices={{}}
layoutApi={layout}
services={{}}
/>
</DataProvider>,
)

expect(wrapper.find(TestComponentWithData).text()).toBe('Loading')
await new Promise(resolve => setTimeout(resolve))
await new Promise(resolve => setTimeout(resolve))

const component = wrapper.update().find(TestComponentWithData)
expect(component.text()).toBe('Length: 3')
Expand Down
13 changes: 6 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
RenderFunctionServices,
ComponentRegistration,
RenderFunction,
} from 'react-json-page-layout'
} from 'json-react-layouts'
import { ComponentState, LoadArguments, DataDefinition, MaybeLoaded } from './DataLoading'
import { DataLoaderResources } from 'react-ssr-data-loader'

Expand Down Expand Up @@ -37,6 +37,7 @@ export function init<LoadDataServices>(
) => ComponentRegistration<TType, TProps & { dataDefinitionArgs: TConfig }, LoadDataServices>
middleware: ComponentRendererMiddleware<LoadDataServices, {}>
} {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const ComponentDataLoader = resources.registerResource<any, LoadArguments<LoadDataServices>>(
'component-data-loader',
params => {
Expand Down Expand Up @@ -77,17 +78,15 @@ export function init<LoadDataServices>(
return registrationWithData
},
middleware: (componentProps, middlewareProps, services, next) => {
const componentDataDefinition = services.routeBuilder.compositionRegistrar.componentRegistrar.get(
componentProps.type,
)
const componentRegistrar = (services.layout as any).compositionRegistrar
.componentRegistrar
const componentDataDefinition = componentRegistrar.get(componentProps.componentType)

const dataDefinition = (componentDataDefinition as any).dataDefinition
if (dataDefinition) {
return (
<ComponentDataLoader
componentRegistrar={
services.routeBuilder.compositionRegistrar.componentRegistrar
}
layout={services.layout}
componentRenderPath={componentProps.componentRenderPath}
dataDefinition={dataDefinition}
dataDefinitionArgs={componentProps.dataDefinitionArgs}
Expand Down
25 changes: 16 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.2.tgz#c4e63af5e8823ce9cc3f0b34f7b998c2171f0c44"
integrity sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg==

"@types/node@^10.14.16":
version "10.14.16"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.16.tgz#4d690c96cbb7b2728afea0e260d680501b3da5cf"
integrity sha512-/opXIbfn0P+VLt+N8DE4l8Mn8rbhiJgabU96ZJ0p9mxOkIks5gh6RUnpHak7Yh0SFkyjO/ODbxsQQPV2bpMmyA==
"@types/node@^12.7.3":
version "12.7.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.3.tgz#27b3f40addaf2f580459fdb405222685542f907a"
integrity sha512-3SiLAIBkDWDg6vFo0+5YJyHPWU9uwu40Qe+v+0MH8wRKYBimHvvAOyk3EzMrD/TrIlLYfXrqDqrg913PynrMJQ==

"@types/prop-types@*":
version "15.7.1"
Expand Down Expand Up @@ -2704,6 +2704,13 @@ json-parse-better-errors@^1.0.1:
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
integrity sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==

json-react-layouts@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/json-react-layouts/-/json-react-layouts-1.0.2.tgz#333756c95ed3df7ea7cd73533049ed87af6c7b46"
integrity sha512-2oCQ9NFJV9mk3hXIIEft5f7Pk02YlYHSW/2qBa23C36pqubjopAakgMOboyfStLm11tMmPfc6Oy0wDMbLl96LA==
dependencies:
typescript-log "^1.1.1"

json-schema-traverse@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660"
Expand Down Expand Up @@ -4353,12 +4360,12 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typescript-log@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/typescript-log/-/typescript-log-1.1.0.tgz#921e643c2a33c845d70123ae2bccdf684e77f004"
integrity sha512-djOXJzoFBm48O4EBgWrP8vPjNBN60DBjgy8jcCY6LguLrL4+K7Y5wY1DjHT2ai1eeA2CwBloPW5Hf3NTF0S7Kg==
typescript-log@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/typescript-log/-/typescript-log-1.1.1.tgz#36952d41d135a92b24f0ed87c3ea8f49dd7aeb8e"
integrity sha512-BEP3PcTcaKTDOy/36qcoF+mgVfTjz70E0Z4+fxt+WEDsNSMXbb8gDJU7W16AWnjqaVY9naMm5WyLbqQ5cb9PoA==

typescript@^3.5.3:
[email protected]:
version "3.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==
Expand Down

0 comments on commit 58122e6

Please sign in to comment.