Skip to content

Commit

Permalink
test: add ImgixProvider unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luqven committed Jun 17, 2021
1 parent cb7608b commit cef074a
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions test/unit/imgix-provider.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { mount, shallow } from 'enzyme';
import React from 'react';
import ReactImgix, { ImgixProvider } from "../../src/index"

const providerProps = {
domain: "sdk-test.imgix.net",
sizes: "100vw",
}

const imageProps = {
src: "https://assets.imgix.net/examples/pione.jpg",
sizes: "50vw",
}

describe('ImgixProvider', () => {

test('should not have context value defined if Provider has no props', () => {
const wrappedComponent = (
<ImgixProvider>
<ReactImgix {...imageProps}/>
</ImgixProvider>
)

const expectedProps = {
children: < ReactImgix src="https://assets.imgix.net/examples/pione.jpg" sizes="50vw" />,
value: {}
}

const renderedComponent = shallow(wrappedComponent)
expect(renderedComponent.props()).toEqual(expectedProps)
})

test('should set the context value to the Provider props', () => {
const wrappedComponent = (
<ImgixProvider {...providerProps}>
<ReactImgix {...imageProps}/>
</ImgixProvider>
)

// ensure Provider value correct set
const expectedProps = {
children: < ReactImgix src="https://assets.imgix.net/examples/pione.jpg" sizes="50vw" />,
value: { domain: "sdk-test.imgix.net", sizes: "100vw", }
}

const renderedComponent = shallow(wrappedComponent)
expect(renderedComponent.props()).toEqual(expectedProps)
})

test('should merge the Provider and Child props', () => {

const modifiedProps = { ...imageProps };
modifiedProps.src = "examples/pione.jpg"
modifiedProps.sizes = null

const wrappedComponent = (
<ImgixProvider {...providerProps}>
<ReactImgix {...modifiedProps}/>
</ImgixProvider>
)

// ensure Provider and Child props are merged as intended
const expectedProps = {
disableSrcSet: false,
domain: "sdk-test.imgix.net",
height: null,
imgixParams: undefined,
onMounted: undefined,
sizes: "100vw",
src: "https://sdk-test.imgix.net/examples/pione.jpg",
width: null,
}

const renderedComponent = mount(wrappedComponent)
const renderedProps = renderedComponent
.childAt(0) // mergePropsHOF
.childAt(0) // processPropsHOF
.childAt(0) // ChildComponent
.props()
// remove noop function that breaks tests
renderedProps.onMounted = undefined

expect(renderedProps).toEqual(expectedProps)
})

test('should log error when has no consumers', () => {
jest.spyOn(global.console, 'error').mockImplementation(() => {})

const wrappedComponent = (
<ImgixProvider {...providerProps}>
</ImgixProvider>
)

const expectedProps = {
children: undefined,
value: providerProps
}

const renderedComponent = shallow(wrappedComponent)

expect(renderedComponent.props()).toEqual(expectedProps)
expect(console.error).toHaveBeenCalledWith(
expect.stringContaining("ImgixProvider must have at least one Imgix child component")
);
})
})

0 comments on commit cef074a

Please sign in to comment.