Skip to content

Commit

Permalink
fix lint and test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
solkimicreb committed Feb 6, 2019
1 parent c072c02 commit 3c1e285
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
3 changes: 3 additions & 0 deletions __mocks__/react-dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// this is here to avoid duplicate react entries in the examples
// (one from the example's node_modules and one from the root's node_modules)
module.exports = require('react-dom')
3 changes: 3 additions & 0 deletions __mocks__/react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// this is here to avoid duplicate react entries in the examples
// (one from the example's node_modules and one from the root's node_modules)
module.exports = require('react-native')
9 changes: 7 additions & 2 deletions __tests__/Clock.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { StrictMode } from 'react'
import { act } from 'react-dom/test-utils'
import { render, cleanup, flushEffects } from 'react-testing-library'
import sinon from 'sinon'
import App from '../examples/clock/src/App'
Expand All @@ -24,10 +25,14 @@ describe('Clock App', () => {
test('should update to display the current time every second', () => {
expect(container).toHaveTextContent('12:00:00 AM')

clock.tick(2000)
act(() => {
clock.tick(2000)
})
expect(container).toHaveTextContent('12:00:02 AM')

clock.tick(8500)
act(() => {
clock.tick(8500)
})
expect(container).toHaveTextContent('12:00:10 AM')
})

Expand Down
9 changes: 7 additions & 2 deletions __tests__/router.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from 'react'
import { act } from 'react-dom/test-utils'
import { render, cleanup, fireEvent } from 'react-testing-library'
import { view, store } from 'react-easy-state'
import {
Expand All @@ -25,7 +26,9 @@ describe('withRouter interaction', () => {
</Router>
)
expect(container).toHaveTextContent('0')
counter.num++
act(() => {
counter.num++
})
expect(container).toHaveTextContent('1')
})

Expand Down Expand Up @@ -70,7 +73,9 @@ describe('withRouter interaction', () => {
</Router>
)
expect(container).toHaveTextContent('0')
counter.num++
act(() => {
counter.num++
})
expect(container).toHaveTextContent('1')
})

Expand Down
9 changes: 7 additions & 2 deletions __tests__/styled.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from 'react'
import { act } from 'react-dom/test-utils'
import { render, cleanup } from 'react-testing-library'
import { view, store } from 'react-easy-state'
import { withTheme, ThemeProvider } from 'styled-components'
Expand All @@ -24,7 +25,9 @@ describe('withRouter interaction', () => {
</Theme>
)
expect(container).toHaveTextContent('0')
counter.num++
act(() => {
counter.num++
})
expect(container).toHaveTextContent('1')
})

Expand Down Expand Up @@ -61,7 +64,9 @@ describe('withRouter interaction', () => {
</Theme>
)
expect(container).toHaveTextContent('0')
counter.num++
act(() => {
counter.num++
})
expect(container).toHaveTextContent('1')
})

Expand Down
18 changes: 10 additions & 8 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { hasHooks } from './utils'
export let isInsideFunctionComponent = false
const COMPONENT = Symbol('owner component')

export default function view(Comp) {
export default function view (Comp) {
const isStatelessComp = !(Comp.prototype && Comp.prototype.isReactComponent)

let ReactiveComp
Expand Down Expand Up @@ -47,7 +47,7 @@ export default function view(Comp) {
// a HOC which overwrites render, shouldComponentUpdate and componentWillUnmount
// it decides when to run the new reactive methods and when to proxy to the original methods
class ReactiveClassComp extends BaseComp {
constructor(props, context) {
constructor (props, context) {
super(props, context)

this.state = this.state || {}
Expand All @@ -60,12 +60,14 @@ export default function view(Comp) {
})
}

render() {
return isStatelessComp ? Comp(this.props, this.context) : super.render()
render () {
return isStatelessComp
? Comp(this.props, this.context)
: super.render()
}

// react should trigger updates on prop changes, while easyState handles store changes
shouldComponentUpdate(nextProps, nextState) {
shouldComponentUpdate (nextProps, nextState) {
const { props, state } = this

// respect the case when the user defines a shouldComponentUpdate
Expand All @@ -88,7 +90,7 @@ export default function view(Comp) {
}

// add a custom deriveStoresFromProps lifecyle method
static getDerivedStateFromProps(props, state) {
static getDerivedStateFromProps (props, state) {
if (super.deriveStoresFromProps) {
// inject all local stores and let the user mutate them directly
const stores = mapStateToStores(state)
Expand All @@ -101,7 +103,7 @@ export default function view(Comp) {
return null
}

componentWillUnmount() {
componentWillUnmount () {
// call user defined componentWillUnmount
if (super.componentWillUnmount) {
super.componentWillUnmount()
Expand All @@ -126,7 +128,7 @@ export default function view(Comp) {
return ReactiveComp
}

function mapStateToStores(state) {
function mapStateToStores (state) {
// find store properties and map them to their none observable raw value
// to do not trigger none static this.setState calls
// from the static getDerivedStateFromProps lifecycle method
Expand Down

0 comments on commit 3c1e285

Please sign in to comment.