-
Notifications
You must be signed in to change notification settings - Fork 58
feat: reader activation segments #4604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+158
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b9dfa1
feat: reader activation segments
miguelpeixe b5e12f7
fix: address review feedback on segments module
miguelpeixe f293b03
merge trunk
miguelpeixe 70e20e7
Merge branch 'trunk' into feat/reader-activation-segments
miguelpeixe c6e3971
fix: address review feedback on segments module
miguelpeixe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| import { EVENTS, emit } from './events'; | ||
|
|
||
| let allSegments = {}; | ||
| let matchedSegment = null; | ||
|
|
||
| /** | ||
| * Register segment definitions. | ||
| * | ||
| * @param {Object} segments Segments keyed by ID with { name, criteria, priority } values. | ||
| */ | ||
| function register( segments ) { | ||
| if ( ! segments || typeof segments !== 'object' ) { | ||
| return; | ||
| } | ||
| const hadMatch = matchedSegment && ! allSegments[ matchedSegment ]; | ||
| allSegments = { ...allSegments, ...segments }; | ||
| if ( hadMatch && allSegments[ matchedSegment ] ) { | ||
| emit( EVENTS.segment, { segmentId: matchedSegment, segment: allSegments[ matchedSegment ], all: { ...allSegments } } ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Set the matched segment for the current reader. | ||
| * | ||
| * @param {string|null} segmentId Segment ID or null to clear. | ||
| * | ||
| * @return {Object|null} Matched segment object or null. | ||
| */ | ||
| function setMatch( segmentId = null ) { | ||
| const normalizedId = segmentId !== null && segmentId !== undefined ? String( segmentId ) : null; | ||
| if ( normalizedId === matchedSegment ) { | ||
| return getMatch(); | ||
| } | ||
| matchedSegment = normalizedId; | ||
| const segment = matchedSegment ? allSegments[ matchedSegment ] || null : null; | ||
| emit( EVENTS.segment, { segmentId: matchedSegment, segment, all: { ...allSegments } } ); | ||
|
miguelpeixe marked this conversation as resolved.
|
||
| return getMatch(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the matched segment. | ||
| * | ||
| * @return {Object|null} Matched segment object with id, or null. | ||
| */ | ||
| function getMatch() { | ||
| if ( ! matchedSegment || ! allSegments[ matchedSegment ] ) { | ||
| return null; | ||
| } | ||
| return { id: matchedSegment, ...allSegments[ matchedSegment ] }; | ||
| } | ||
|
|
||
| /** | ||
| * Get all registered segments. | ||
| * | ||
| * @return {Object} Segments keyed by ID. | ||
| */ | ||
| function getAll() { | ||
| return { ...allSegments }; | ||
| } | ||
|
|
||
| /** | ||
| * Reset module state. For testing only. | ||
| */ | ||
| export function reset() { | ||
| allSegments = {}; | ||
| matchedSegment = null; | ||
| } | ||
|
|
||
| export default { | ||
|
miguelpeixe marked this conversation as resolved.
|
||
| register, | ||
| setMatch, | ||
| getMatch, | ||
| getAll, | ||
| }; | ||
This file contains hidden or 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,81 @@ | ||
| import { on, off } from './events'; | ||
| import segments, { reset } from './segments'; | ||
|
|
||
| const sampleSegments = { | ||
| 42: { name: 'Loyal Readers', criteria: [ { criteria_id: 'articles_read', value: { min: 5 } } ], priority: 0 }, | ||
| 43: { name: 'New Visitors', criteria: [ { criteria_id: 'articles_read', value: { max: 2 } } ], priority: 1 }, | ||
| }; | ||
|
|
||
| describe( 'segments', () => { | ||
| beforeEach( () => { | ||
| reset(); | ||
| } ); | ||
| it( 'should return empty object initially', () => { | ||
| expect( segments.getAll() ).toEqual( {} ); | ||
| } ); | ||
| it( 'should return null for match initially', () => { | ||
| expect( segments.getMatch() ).toBeNull(); | ||
| } ); | ||
| it( 'should register segments and retrieve them', () => { | ||
| segments.register( sampleSegments ); | ||
| const all = segments.getAll(); | ||
| expect( all[ '42' ].name ).toBe( 'Loyal Readers' ); | ||
| expect( all[ '43' ].name ).toBe( 'New Visitors' ); | ||
| } ); | ||
| it( 'should set match and emit segment event', () => { | ||
| const callback = jest.fn(); | ||
| on( 'segment', callback ); | ||
| segments.register( sampleSegments ); | ||
| segments.setMatch( '42' ); | ||
| expect( callback ).toHaveBeenCalled(); | ||
| const detail = callback.mock.calls[ 0 ][ 0 ].detail; | ||
|
miguelpeixe marked this conversation as resolved.
|
||
| expect( detail.segmentId ).toBe( '42' ); | ||
| expect( detail.segment.name ).toBe( 'Loyal Readers' ); | ||
| expect( detail.all ).toEqual( expect.objectContaining( { 42: expect.any( Object ) } ) ); | ||
| off( 'segment', callback ); | ||
| } ); | ||
| it( 'should not re-emit when setting same match', () => { | ||
| const callback = jest.fn(); | ||
| on( 'segment', callback ); | ||
| segments.register( sampleSegments ); | ||
| segments.setMatch( '42' ); | ||
| callback.mockClear(); | ||
| segments.setMatch( '42' ); | ||
| expect( callback ).not.toHaveBeenCalled(); | ||
| off( 'segment', callback ); | ||
| } ); | ||
|
miguelpeixe marked this conversation as resolved.
|
||
| it( 'should return matched segment via getMatch', () => { | ||
| segments.register( sampleSegments ); | ||
| segments.setMatch( '42' ); | ||
| const match = segments.getMatch(); | ||
| expect( match.id ).toBe( '42' ); | ||
| expect( match.name ).toBe( 'Loyal Readers' ); | ||
| expect( match.priority ).toBe( 0 ); | ||
| } ); | ||
| it( 'should clear match and emit event', () => { | ||
| const callback = jest.fn(); | ||
| on( 'segment', callback ); | ||
| segments.register( sampleSegments ); | ||
| segments.setMatch( '42' ); | ||
| callback.mockClear(); | ||
| segments.setMatch( null ); | ||
|
miguelpeixe marked this conversation as resolved.
|
||
| expect( segments.getMatch() ).toBeNull(); | ||
| expect( callback ).toHaveBeenCalled(); | ||
| expect( callback.mock.calls[ 0 ][ 0 ].detail.segmentId ).toBeNull(); | ||
| off( 'segment', callback ); | ||
| } ); | ||
| it( 'should re-emit when register resolves a pending match', () => { | ||
| const callback = jest.fn(); | ||
| on( 'segment', callback ); | ||
| segments.setMatch( '42' ); | ||
| expect( segments.getMatch() ).toBeNull(); | ||
| callback.mockClear(); | ||
| segments.register( sampleSegments ); | ||
| expect( callback ).toHaveBeenCalled(); | ||
| const detail = callback.mock.calls[ 0 ][ 0 ].detail; | ||
| expect( detail.segmentId ).toBe( '42' ); | ||
| expect( detail.segment.name ).toBe( 'Loyal Readers' ); | ||
| expect( segments.getMatch().id ).toBe( '42' ); | ||
| off( 'segment', callback ); | ||
| } ); | ||
| } ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.