Skip to content

Commit

Permalink
Merge d01bccd into b46885c
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 15, 2019
2 parents b46885c + d01bccd commit 412e3ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
59 changes: 58 additions & 1 deletion hooks/test/browser/useContext.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { h, render, createContext, Component } from 'preact';
import { act } from 'preact/test-utils';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { useContext, useEffect } from '../../src';
import { useContext, useEffect, useState } from '../../src';

/** @jsx h */

Expand Down Expand Up @@ -163,4 +164,60 @@ describe('useContext', () => {
expect(spy).to.be.calledTwice;
expect(unmountspy).not.to.be.called;
});

it('should maintain context', done => {
const context = createContext(null);
const { Provider } = context;
const first = { name: 'first' };
const second = { name: 'second' };

const Input = () => {
const config = useContext(context);

// Avoid eslint complaining about unused first value
const state = useState('initial');
const set = state[1];

useEffect(() => {
// Schedule the update on the next frame
requestAnimationFrame(() => {
set('irrelevant');
});
}, [config]);

return (
<div>
{config.name}
</div>
);
};

const App = (props) => {
const [config, setConfig] = useState({});

useEffect(() => {
setConfig(props.config);
}, [props.config]);

return (
<Provider value={config}>
<Input />
</Provider>
);
};

act(() => {
render(<App config={first} />, scratch);

// Create a new div to append the `second` case
const div = scratch.appendChild(document.createElement('div'));
render(<App config={second} />, div);
});

// Push the expect into the next frame
requestAnimationFrame(() => {
expect(scratch.innerHTML).equal('<div>first</div><div><div>second</div></div>');
done();
});
});
});
1 change: 0 additions & 1 deletion src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export function createContext(defaultValue) {
};
this.shouldComponentUpdate = _props => {
if (props.value !== _props.value) {
ctx[context._id].props.value = _props.value;
subs.some(c => {
// Check if still mounted
if (c._parentDom) {
Expand Down

0 comments on commit 412e3ce

Please sign in to comment.