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

[PE-4862] Updates background component to merge props #932

Merged
merged 2 commits into from
Dec 11, 2023
Merged
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 .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
command: yarn build
deploy:
docker:
- image: cimg/node:20.4
- image: cimg/node:21.4
steps:
- checkout
- node/install-packages
Expand Down
6 changes: 5 additions & 1 deletion src/react-imgix-bg.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import constructUrl from "./constructUrl";
import extractQueryParams from "./extractQueryParams";
import findClosest from "./findClosest";
import targetWidths from "./targetWidths";
import { mergeComponentPropsHOF, processPropsHOF } from "./HOFs";

const findNearestWidth = (actualWidth) =>
findClosest(actualWidth, targetWidths);
Expand Down Expand Up @@ -188,6 +189,9 @@ class BackgroundImpl extends React.Component {
);
}
}
const Background = withContentRect("bounds")(BackgroundImpl);

const Background = mergeComponentPropsHOF(
processPropsHOF(withContentRect("bounds")(BackgroundImpl))
);

export { Background, BackgroundImpl as __BackgroundImpl };
82 changes: 57 additions & 25 deletions test/unit/imgix-provider.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount, shallow } from "enzyme";
import React from "react";
import ReactImgix, { ImgixProvider } from "../../src/index";
import ReactImgix, { ImgixProvider, Background } from "../../src/index";

const providerProps = {
domain: "sdk-test.imgix.net",
Expand All @@ -17,43 +17,57 @@ describe("ImgixProvider", () => {
const wrappedComponent = (
<ImgixProvider>
<ReactImgix {...imageProps} />
<Background {...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);

// Inspect the rendered children directly
const renderedChildren = renderedComponent.children();

expect(renderedChildren.length).toBe(2); // Assuming you have two children
expect(renderedChildren.at(0).props()).toEqual({
src: 'https://assets.imgix.net/examples/pione.jpg',
longevitytina marked this conversation as resolved.
Show resolved Hide resolved
sizes: imageProps.sizes,
});

expect(renderedChildren.at(1).props()).toEqual({
src: 'https://assets.imgix.net/examples/pione.jpg',
sizes: imageProps.sizes,
});

expect(renderedComponent.prop('value')).toEqual({});
});

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

// ensure Provider value correctly set
const renderedComponent = shallow(wrappedComponent);

// Inspect the rendered children directly
const renderedChildren = renderedComponent.children();

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);
expect(renderedChildren.length).toBe(2); // Assuming you have two children
expect(renderedChildren.at(0).props()).toEqual({
src: 'https://assets.imgix.net/examples/pione.jpg',
sizes: '50vw',
});
expect(renderedChildren.at(1).props()).toEqual({
src: 'https://assets.imgix.net/examples/pione.jpg',
sizes: '50vw',
});

expect(renderedComponent.prop('value')).toEqual(expectedProps.value);
});

test("should merge the Provider and Child props", () => {
Expand All @@ -66,33 +80,51 @@ describe("ImgixProvider", () => {
const wrappedComponent = (
<ImgixProvider {...providerProps}>
<ReactImgix {...modifiedProps} />
<Background {...modifiedProps} />
longevitytina marked this conversation as resolved.
Show resolved Hide resolved
</ImgixProvider>
);

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

const expectedReactImgixProps = {
...expectedProps,
disableSrcSet: false,
};

const expectedBgProps = {
...expectedProps,
};

// The order of the childAt() needs to update if number of HOCs change.
const renderedComponent = mount(wrappedComponent);
const renderedComponent = mount(wrappedComponent); //ImgixProvider

const renderedProps = renderedComponent
.childAt(0) // mergePropsHOF
.childAt(0) // processPropsHOF
.childAt(0) // shouldComponentUpdateHOC
.childAt(0) // ChildComponent
.props();

const renderedBackgroundProps = renderedComponent
.childAt(1) // mergePropsHOF
.childAt(0) // processPropsHOF
.childAt(0) // withContentRect
.props();

// remove noop function that breaks tests
renderedProps.onMounted = undefined;

expect(renderedProps).toEqual(expectedProps);
expect(renderedProps).toEqual(expectedReactImgixProps);
expect(renderedBackgroundProps).toEqual(expectedBgProps);
});

test("should log error when has no consumers", () => {
Expand Down