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 onChange callback #95

Merged
merged 4 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion modules/Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Media extends React.Component {
]).isRequired,
render: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
targetWindow: PropTypes.object
targetWindow: PropTypes.object,
onQueryStateChange: PropTypes.func
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to rename it to onChange, as per @mjackson's comment.

};

static defaultProps = {
Expand All @@ -43,12 +44,23 @@ class Media extends React.Component {
if (typeof query !== "string") query = json2mq(query);

this.mediaQueryList = targetWindow.matchMedia(query);

const { onQueryStateChange } = this.props;
if (onQueryStateChange) {
this.mediaQueryList.addListener(onQueryStateChange);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a listener registered in updateMatches. We should call the onChange callback there instead of registering it as a separate listener.

}

this.mediaQueryList.addListener(this.updateMatches);
this.updateMatches();
}

componentWillUnmount() {
this.mediaQueryList.removeListener(this.updateMatches);

const { onQueryStateChange } = this.props;
if (onQueryStateChange) {
this.mediaQueryList.removeListener(onQueryStateChange);
}
}

render() {
Expand Down
34 changes: 31 additions & 3 deletions modules/__tests__/Media-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import ReactDOM from "react-dom";
import ReactDOMServer from "react-dom/server";
import Media from "../Media";

const createMockMediaMatcher = matches => () => ({
const createMockMediaMatcher = (
matches,
addListener: () => {},
removeListener: () => {}
addListener = () => {},
removeListener = () => {}
) => () => ({
matches,
addListener,
removeListener
});

describe("A <Media>", () => {
Expand Down Expand Up @@ -160,6 +164,30 @@ describe("A <Media>", () => {
});
});

describe("when an onQueryStateChange function is passed", () => {
const mockAddListener = jest.fn();
beforeEach(() => {
window.matchMedia = createMockMediaMatcher(true, mockAddListener);
});

afterEach(() => {
mockAddListener.mockClear();
});

it("adds the function as a listener to the media query", () => {
const callback = () => {};
const element = (
<Media query="" onQueryStateChange={callback}>
{matches => (matches ? <div>hello</div> : <div>goodbye</div>)}
</Media>
);

ReactDOM.render(element, node, () => {
expect(mockAddListener).toHaveBeenCalledWith(callback);
});
});
});

describe("rendered on the server", () => {
beforeEach(() => {
window.matchMedia = createMockMediaMatcher(true);
Expand Down