This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
329 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
|
||
import withMatchMediaProps from '../src/'; | ||
|
||
const Demo = (props) => ( | ||
<h1>props: {JSON.stringify(props)}</h1> | ||
); | ||
|
||
export default withMatchMediaProps({ | ||
isSmallScreen: { | ||
maxWidth: 500 | ||
}, | ||
isHighDpiScreen: { | ||
minResolution: '192dpi' | ||
} | ||
})(Demo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@hocs/with-match-media-props", | ||
"library": "withMatchMediaProps", | ||
"version": "0.1.0", | ||
"description": "CSS Media Queries HOC for React", | ||
"keywords": [ | ||
"react", | ||
"hoc", | ||
"recompose" | ||
], | ||
"main": "lib/index.js", | ||
"module": "es/index.js", | ||
"files": [ | ||
"dist/", | ||
"es/", | ||
"lib/" | ||
], | ||
"repository": "deepsweet/hocs", | ||
"author": "Kir Belevich <[email protected]> (https://github.com/deepsweet)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"json2mq": "^0.2.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "^15.6.1", | ||
"recompose": "^0.24.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { Component } from 'react'; | ||
import { setDisplayName, wrapDisplayName } from 'recompose'; | ||
import json2mq from 'json2mq'; | ||
|
||
const queryToMql = (query) => global.matchMedia(json2mq(query)); | ||
const createMediaMatcher = (query) => { | ||
const mql = queryToMql(query); | ||
|
||
return { | ||
matches: mql.matches, | ||
subscribe(handler) { | ||
mql.addListener(handler); | ||
|
||
return () => mql.removeListener(handler); | ||
} | ||
}; | ||
}; | ||
|
||
const withMatchMediaProps = (propsQieries = {}) => (Target) => { | ||
class WithMatchMediaProps extends Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
|
||
this.propsMatchers = Object.keys(propsQieries).map((prop) => ({ | ||
prop, | ||
...createMediaMatcher(propsQieries[prop]) | ||
})); | ||
|
||
this.state = this.propsMatchers.reduce((result, propMatcher) => ({ | ||
...result, | ||
[propMatcher.prop]: propMatcher.matches | ||
}), {}); | ||
} | ||
|
||
componentDidMount() { | ||
this.unsubscribers = this.propsMatchers.map((propMatcher) => propMatcher.subscribe((e) => { | ||
this.setState({ | ||
[propMatcher.prop]: e.matches | ||
}); | ||
})); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.unsubscribers.forEach((unsubscribe) => unsubscribe()); | ||
} | ||
|
||
render() { | ||
return ( | ||
<Target {...this.props} {...this.state}/> | ||
); | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
return setDisplayName(wrapDisplayName(Target, 'withMatchMediaProps'))(WithMatchMediaProps); | ||
} | ||
|
||
return WithMatchMediaProps; | ||
}; | ||
|
||
export default withMatchMediaProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
const Target = () => null; | ||
|
||
describe('withMatchMediaProps', () => { | ||
const mockJson2mq = jest.fn(); | ||
let withMatchMediaProps = null; | ||
|
||
beforeAll(() => { | ||
withMatchMediaProps = require('../src/').default; | ||
|
||
jest.mock('json2mq', () => mockJson2mq); | ||
}); | ||
|
||
beforeEach(() => { | ||
mockJson2mq.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.unmock('json2mq'); | ||
}); | ||
|
||
it('should just pass props through when called without arguments', () => { | ||
const EnchancedTarget = withMatchMediaProps()(Target); | ||
const wrapper = mount( | ||
<EnchancedTarget a={1} b={2} c={3}/> | ||
); | ||
|
||
expect(wrapper.find(Target).props()).toEqual({ a: 1, b: 2, c: 3 }); | ||
}); | ||
|
||
describe('`window.matchMedia`', () => { | ||
let originMatchMedia = null; | ||
|
||
beforeAll(() => { | ||
originMatchMedia = global.matchMedia; | ||
}); | ||
|
||
afterAll(() => { | ||
global.matchMedia = originMatchMedia; | ||
}); | ||
|
||
it('should pass query object to json2mq', () => { | ||
global.matchMedia = jest.fn(() => ({ | ||
addListener: () => {}, | ||
removeListener: () => {}, | ||
matches: true | ||
})); | ||
|
||
const EnchancedTarget = withMatchMediaProps({ | ||
test: { | ||
maxWidth: 300 | ||
} | ||
})(Target); | ||
|
||
mount( | ||
<EnchancedTarget/> | ||
); | ||
|
||
expect(mockJson2mq).toHaveBeenCalledTimes(1); | ||
expect(mockJson2mq).toHaveBeenCalledWith({ maxWidth: 300 }); | ||
}); | ||
|
||
it('should set initial state and provide props with matched queries', () => { | ||
global.matchMedia = jest.fn(() => ({ | ||
addListener: () => {}, | ||
removeListener: () => {}, | ||
matches: true | ||
})); | ||
|
||
const EnchancedTarget = withMatchMediaProps({ | ||
test: { | ||
maxWidth: 300 | ||
} | ||
})(Target); | ||
const wrapper = mount( | ||
<EnchancedTarget/> | ||
); | ||
|
||
expect(wrapper.find(Target).prop('test')).toBe(true); | ||
}); | ||
|
||
it('should subscribe on mount and unsubscribe on unmount', () => { | ||
const mockAddListener = jest.fn(); | ||
const mockRemoveListener = jest.fn(); | ||
|
||
global.matchMedia = jest.fn(() => ({ | ||
addListener: mockAddListener, | ||
removeListener: mockRemoveListener | ||
})); | ||
|
||
const EnchancedTarget = withMatchMediaProps({ | ||
test: { | ||
maxWidth: 300 | ||
} | ||
})(Target); | ||
const wrapper = mount( | ||
<EnchancedTarget/> | ||
); | ||
|
||
expect(mockAddListener).toHaveBeenCalledTimes(1); | ||
wrapper.unmount(); | ||
expect(mockRemoveListener).toHaveBeenCalledTimes(1); | ||
expect(mockRemoveListener).toHaveBeenCalledWith(mockAddListener.mock.calls[0][0]); | ||
}); | ||
|
||
it('should change state and provide props when query has been matched', () => { | ||
const mockAddListener = jest.fn(); | ||
|
||
global.matchMedia = jest.fn(() => ({ | ||
addListener: mockAddListener, | ||
removeListener: () => {}, | ||
matches: false | ||
})); | ||
|
||
const EnchancedTarget = withMatchMediaProps({ | ||
test: { | ||
maxWidth: 300 | ||
} | ||
})(Target); | ||
const wrapper = mount( | ||
<EnchancedTarget/> | ||
); | ||
|
||
mockAddListener.mock.calls[0][0]({ matches: true }); | ||
expect(wrapper.find(Target).prop('test')).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('display name', () => { | ||
let origNodeEnv = null; | ||
|
||
beforeAll(() => { | ||
origNodeEnv = process.env.NODE_ENV; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env.NODE_ENV = origNodeEnv; | ||
}); | ||
|
||
it('should wrap display name in non-production env', () => { | ||
process.env.NODE_ENV = 'test'; | ||
|
||
const EnchancedTarget = withMatchMediaProps()(Target); | ||
const wrapper = mount( | ||
<EnchancedTarget/> | ||
); | ||
|
||
expect(wrapper.name()).toBe('withMatchMediaProps(Target)'); | ||
}); | ||
|
||
it('should not wrap display name in production env', () => { | ||
process.env.NODE_ENV = 'production'; | ||
|
||
const EnchancedTarget = withMatchMediaProps()(Target); | ||
const wrapper = mount( | ||
<EnchancedTarget/> | ||
); | ||
|
||
expect(wrapper.name()).toBe('WithMatchMediaProps'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
json2mq@^0.2.0: | ||
version "0.2.0" | ||
resolved "https://registry.yarnpkg.com/json2mq/-/json2mq-0.2.0.tgz#b637bd3ba9eabe122c83e9720483aeb10d2c904a" | ||
dependencies: | ||
string-convert "^0.2.0" | ||
|
||
string-convert@^0.2.0: | ||
version "0.2.1" | ||
resolved "https://registry.yarnpkg.com/string-convert/-/string-convert-0.2.1.tgz#6982cc3049fbb4cd85f8b24568b9d9bf39eeff97" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters