From ff2b89e4b2c6902b8299ced065baf21f7e51aa09 Mon Sep 17 00:00:00 2001 From: Anton Lavrenov Date: Sat, 3 Jun 2023 16:54:56 -0500 Subject: [PATCH] add test for issue #748 --- test/react-konva-test.tsx | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/test/react-konva-test.tsx b/test/react-konva-test.tsx index 08d8756..4493066 100644 --- a/test/react-konva-test.tsx +++ b/test/react-konva-test.tsx @@ -1,5 +1,6 @@ import React from 'react'; import { createRoot } from 'react-dom/client'; +import { unstable_batchedUpdates as batchedUpdates } from 'react-dom'; import sinon from 'sinon'; import { expect } from 'chai'; import Konva from 'konva'; @@ -1264,3 +1265,76 @@ describe('external', () => { expect(typeof stage.findOne('Rect')._applyProps).to.equal('function'); }); }); + +describe('update order', () => { + const store = { + listeners: [], + state: { + name: 'test', + }, + getState() { + return store.state; + }, + subscribe(cb) { + this.listeners.push(cb); + }, + async dispatch() { + await Promise.resolve(); + + this.state = { + name: 'test2', + }; + this.listeners.forEach((cb) => cb()); + }, + }; + + function useSelector(selector) { + return React.useSyncExternalStore( + (callback) => store.subscribe(callback), + () => store.getState(), + undefined, + selector + ); + } + + const App = () => { + return ( + + + + ); + }; + + const renderCallStack = []; + function ViewLayer() { + renderCallStack.push('ViewLayer'); + + useSelector((state) => state.name); + + return ( + + + + ); + } + + function ViewText() { + renderCallStack.push('ViewText'); + + const name = useSelector((state) => state.name); + + return ; + } + + it.skip('update order', async function () { + const { stage } = await render(); + await new Promise((resolve) => setTimeout(resolve, 150)); + await store.dispatch(); + expect(renderCallStack).to.deep.equal([ + 'ViewLayer', + 'ViewText', + 'ViewLayer', + 'ViewText', + ]); + }); +});