-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ObservableMap: optimize unsubscribe and add unit tests (#60892)
* ObservableMap: optimize unsubscribe and add unit tests * Use jest.fn to count function calls * Inline the getListeners function after TS upgrade to 5.4.5 Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org> Co-authored-by: ellatrix <ellatrix@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org>
- Loading branch information
1 parent
e33cb88
commit ff3afd3
Showing
2 changed files
with
99 additions
and
15 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,83 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen, act } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
observableMap, | ||
useObservableValue, | ||
} from '../bubbles-virtually/observable-map'; | ||
|
||
describe( 'ObservableMap', () => { | ||
test( 'should observe individual values', () => { | ||
const map = observableMap(); | ||
|
||
const listenerA = jest.fn(); | ||
const listenerB = jest.fn(); | ||
|
||
const unsubA = map.subscribe( 'a', listenerA ); | ||
const unsubB = map.subscribe( 'b', listenerB ); | ||
|
||
// check that setting `a` doesn't notify the `b` listener | ||
map.set( 'a', 1 ); | ||
expect( listenerA ).toHaveBeenCalledTimes( 1 ); | ||
expect( listenerB ).toHaveBeenCalledTimes( 0 ); | ||
|
||
// check that setting `b` doesn't notify the `a` listener | ||
map.set( 'b', 2 ); | ||
expect( listenerA ).toHaveBeenCalledTimes( 1 ); | ||
expect( listenerB ).toHaveBeenCalledTimes( 1 ); | ||
|
||
// check that `delete` triggers notifications, too | ||
map.delete( 'a' ); | ||
expect( listenerA ).toHaveBeenCalledTimes( 2 ); | ||
expect( listenerB ).toHaveBeenCalledTimes( 1 ); | ||
|
||
// check that the subscription survived the `delete` | ||
map.set( 'a', 2 ); | ||
expect( listenerA ).toHaveBeenCalledTimes( 3 ); | ||
expect( listenerB ).toHaveBeenCalledTimes( 1 ); | ||
|
||
// check that unsubscription really works | ||
unsubA(); | ||
unsubB(); | ||
map.set( 'a', 3 ); | ||
expect( listenerA ).toHaveBeenCalledTimes( 3 ); | ||
expect( listenerB ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'useObservableValue', () => { | ||
test( 'reacts only to the specified key', () => { | ||
const map = observableMap(); | ||
map.set( 'a', 1 ); | ||
|
||
const MapUI = jest.fn( () => { | ||
const value = useObservableValue( map, 'a' ); | ||
return <div>value is { value }</div>; | ||
} ); | ||
|
||
render( <MapUI /> ); | ||
expect( screen.getByText( /^value is/ ) ).toHaveTextContent( | ||
'value is 1' | ||
); | ||
expect( MapUI ).toHaveBeenCalledTimes( 1 ); | ||
|
||
act( () => { | ||
map.set( 'a', 2 ); | ||
} ); | ||
expect( screen.getByText( /^value is/ ) ).toHaveTextContent( | ||
'value is 2' | ||
); | ||
expect( MapUI ).toHaveBeenCalledTimes( 2 ); | ||
|
||
// check that setting unobserved map key doesn't trigger a render at all | ||
act( () => { | ||
map.set( 'b', 1 ); | ||
} ); | ||
expect( MapUI ).toHaveBeenCalledTimes( 2 ); | ||
} ); | ||
} ); |
ff3afd3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in ff3afd3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8754292409
📝 Reported issues:
/test/e2e/specs/editor/various/multi-block-selection.spec.js