Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: set two state but re-render once #992

Merged
merged 11 commits into from
Mar 28, 2019
2 changes: 1 addition & 1 deletion packages/driver-dom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export function beforeRender({ hydrate }) {

function recolectHydrationChild(hydrationParent) {
const nativeLength = hydrationParent.childNodes.length;
const vdomLength = hydrationParent[HYDRATION_INDEX];
const vdomLength = hydrationParent[HYDRATION_INDEX] || 0;
if (nativeLength - vdomLength > 0) {
for (let i = nativeLength - 1; i >= vdomLength; i-- ) {
hydrationParent.removeChild(hydrationParent.childNodes[i]);
Expand Down
13 changes: 13 additions & 0 deletions packages/rax-create-portal/src/__tests__/createPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ describe('createPortal', () => {

beforeEach(function() {
Host.driver = ServerDriver;
jest.useFakeTimers();
});

afterEach(function() {
Host.driver = null;
jest.useRealTimers();
});

it('should render one portal', () => {
Expand All @@ -37,6 +39,7 @@ describe('createPortal', () => {
container
);

jest.runAllTimers();
expect(container.childNodes[0].tagName).toBe('DIV');
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('portal');

Expand Down Expand Up @@ -78,6 +81,8 @@ describe('createPortal', () => {
}

render(<Parent />, container);

jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('bar');
});
Expand Down Expand Up @@ -116,10 +121,12 @@ describe('createPortal', () => {
}

render(<Parent bar="initial" />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('initial-initial');

render(<Parent bar="changed" />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('changed-changed');
});
Expand Down Expand Up @@ -159,36 +166,42 @@ describe('createPortal', () => {
}

render(<Parent />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('initial');
expect(updatedCount).toBe(1);

// nothing can be changed
render(<Parent />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('initial');
expect(updatedCount).toBe(1);

// Context change
render(<Parent bar="changed" hasContext={true} />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('changed');
expect(updatedCount).toBe(2);

// change context -> null
render(<Parent />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('initial');
expect(updatedCount).toBe(3);

// nothing can be changed
render(<Parent />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('initial');
expect(updatedCount).toBe(3);

// element change
render(<Parent newElement={true} />, container);
jest.runAllTimers();
expect(container.childNodes[0].nodeType).toBe(8);
expect(portalContainer.childNodes[0].childNodes[0].data).toBe('initial');
expect(updatedCount).toBe(4);
Expand Down
2 changes: 1 addition & 1 deletion packages/rax/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rax",
"version": "1.0.0",
"version": "1.0.1",
"description": "A universal React-compatible render engine.",
"license": "BSD-3-Clause",
"main": "lib/index.js",
Expand Down
6 changes: 6 additions & 0 deletions packages/rax/src/__tests__/createContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ describe('createContext', () => {

beforeEach(function() {
Host.driver = ServerDriver;
jest.useFakeTimers();
});

afterEach(function() {
Host.driver = null;
jest.useRealTimers();
});

it('simple mount and update', () => {
Expand Down Expand Up @@ -112,11 +114,13 @@ describe('createContext', () => {

// Update
render(<App value={3} />, container);
jest.runAllTimers();
expect(container.childNodes[0].childNodes[0].data).toEqual('3');
expect(container.childNodes[1].childNodes[0].data).toEqual('6');

// Another update
render(<App value={4} />, container);
jest.runAllTimers();
expect(container.childNodes[0].childNodes[0].data).toEqual('4');
expect(container.childNodes[1].childNodes[0].data).toEqual('8');
});
Expand Down Expand Up @@ -167,6 +171,7 @@ describe('createContext', () => {

// Update
render(<App value={3} />, container);
jest.runAllTimers();
expect(container.childNodes[0].childNodes[0].childNodes[0].data).toEqual('3');
expect(container.childNodes[0].childNodes[1].childNodes[0].data).toEqual('3');
});
Expand Down Expand Up @@ -202,6 +207,7 @@ describe('createContext', () => {
const spyConsumerRender = jest.spyOn(instance.refs.consumer, 'render');
const spyConsumerWithIndirection = jest.spyOn(instance.refs.consumerWithIndirection, 'render');
render(<App value={3} />, container);
jest.runAllTimers();
expect(spyConsumerRender).toHaveBeenCalledTimes(1);
expect(spyConsumerWithIndirection).toHaveBeenCalledTimes(1);
});
Expand Down
Loading