-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(headless): Refactor the Headless Notify Trigger controller to ma…
…ke it compatible with the insight use case (#4362) [SFINT-5468](https://coveord.atlassian.net/browse/SFINT-5468) ### IN THIS PR: We want to refactor the Headless Notify trigger controller to make it compatible with the insight use case. We therefore will have the Core Triggers controller and have 2 controllers (1 for insight usecase and 1 for search usecase) We are doing the following steps: 1. Created an insight-trigger-analytics-actions.ts file and added the `logNotifyTrigger` action in the file. 2. Added insight-trigger-analytics-actions.test.ts file to test the `logNotifyTrigger` action described above. 3. Modified the Core controller + tests 4. Added a insightTriggerController + tests 5. Modified the search trigger controller + tests 6. updated exports files in index ### DEMO IN HIP: https://github.com/user-attachments/assets/25d25833-7083-45e4-ab2c-98d7638aeba5 ### TESTS: HEADLESS-CORE-NOTIFY-TRIGGER: <img width="300" alt="image" src="https://github.com/user-attachments/assets/4513965e-11df-459e-92d7-0645615d4ec8"> HEADLESS-NOTIFY-TRIGGER: <img width="300" alt="image" src="https://github.com/user-attachments/assets/bd01ceed-8543-4ed2-b7de-a033554a1683"> HEADLESS-INSIGHT-NOTIFY-TRIGGER: <img width="300" alt="image" src="https://github.com/user-attachments/assets/489251e1-dc23-4254-893d-3013610cd291"> INSIGHT-TRIGGER-ANALYTICS-ACTIONS: <img width="300" alt="image" src="https://github.com/user-attachments/assets/435cd04d-2566-47fd-9772-d766ab80b250"> [SFINT-5468]: https://coveord.atlassian.net/browse/SFINT-5468?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
- Loading branch information
1 parent
1f45d27
commit e8e81e1
Showing
12 changed files
with
413 additions
and
170 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
154 changes: 154 additions & 0 deletions
154
packages/headless/src/controllers/core/triggers/headless-core-notify-trigger.test.ts
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,154 @@ | ||
import {Mock} from 'vitest'; | ||
import {logNotifyTrigger} from '../../../features/triggers/trigger-analytics-actions.js'; | ||
import {triggerReducer as triggers} from '../../../features/triggers/triggers-slice.js'; | ||
import { | ||
buildMockSearchEngine, | ||
MockedSearchEngine, | ||
} from '../../../test/mock-engine-v2.js'; | ||
import {createMockState} from '../../../test/mock-state.js'; | ||
import {NotifyTrigger} from '../../core/triggers/headless-core-notify-trigger.js'; | ||
import {buildCoreNotifyTrigger} from './headless-core-notify-trigger.js'; | ||
|
||
vi.mock('../../../features/triggers/trigger-analytics-actions'); | ||
|
||
describe('NotifyTrigger', () => { | ||
let engine: MockedSearchEngine; | ||
let notifyTrigger: NotifyTrigger; | ||
|
||
function initNotifyTrigger() { | ||
notifyTrigger = buildCoreNotifyTrigger(engine, { | ||
options: { | ||
logNotifyTriggerActionCreator: logNotifyTrigger, | ||
}, | ||
}); | ||
} | ||
|
||
function setEngineTriggersState(notifications: string[]) { | ||
engine.state.triggers!.notifications = notifications; | ||
} | ||
|
||
function registeredListeners() { | ||
return (engine.subscribe as Mock).mock.calls.map((args) => args[0]); | ||
} | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
engine = buildMockSearchEngine(createMockState()); | ||
initNotifyTrigger(); | ||
}); | ||
|
||
it('initializes', () => { | ||
expect(notifyTrigger).toBeTruthy(); | ||
}); | ||
|
||
it('it adds the correct reducers to the engine', () => { | ||
expect(engine.addReducers).toHaveBeenCalledWith({ | ||
triggers, | ||
}); | ||
}); | ||
|
||
it('exposes a #subscribe method', () => { | ||
expect(notifyTrigger.subscribe).toBeTruthy(); | ||
}); | ||
|
||
describe('when the #engine.state.triggers.notifications is not updated', () => { | ||
const listener = vi.fn(); | ||
beforeEach(() => { | ||
engine = buildMockSearchEngine(createMockState()); | ||
initNotifyTrigger(); | ||
notifyTrigger.subscribe(listener); | ||
const [firstListener] = registeredListeners(); | ||
firstListener(); | ||
}); | ||
|
||
it('it does not call the listener', () => { | ||
expect(listener).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('it does not dispatch #logNotifyTrigger', () => { | ||
expect(logNotifyTrigger).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('when the #engine.state.triggers.notifications is updated', () => { | ||
const listener = vi.fn(); | ||
beforeEach(() => { | ||
engine = buildMockSearchEngine(createMockState()); | ||
initNotifyTrigger(); | ||
notifyTrigger.subscribe(listener); | ||
setEngineTriggersState(['hello']); | ||
const [firstListener] = registeredListeners(); | ||
firstListener(); | ||
}); | ||
|
||
it('it calls the listener', () => { | ||
expect(listener).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('it dispatches #logNotifyTrigger', () => { | ||
expect(logNotifyTrigger).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('when the #engine.state.triggers.notifications is updated with an empty array', () => { | ||
const listener = vi.fn(); | ||
beforeEach(() => { | ||
engine = buildMockSearchEngine(createMockState()); | ||
initNotifyTrigger(); | ||
notifyTrigger.subscribe(listener); | ||
setEngineTriggersState([]); | ||
const [firstListener] = registeredListeners(); | ||
firstListener(); | ||
}); | ||
|
||
it('it does not call the listener', () => { | ||
expect(listener).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('it does not dispatch #logNotifyTrigger', () => { | ||
expect(logNotifyTrigger).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('when a non-empty #engine.state.triggers.notifications is updated with an empty array', () => { | ||
const listener = vi.fn(); | ||
beforeEach(() => { | ||
engine = buildMockSearchEngine(createMockState()); | ||
setEngineTriggersState(['hello', 'world']); | ||
initNotifyTrigger(); | ||
notifyTrigger.subscribe(listener); | ||
setEngineTriggersState([]); | ||
const [firstListener] = registeredListeners(); | ||
firstListener(); | ||
}); | ||
|
||
it('it calls the listener', () => { | ||
expect(listener).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('it dispatches #logNotifyTrigger', () => { | ||
expect(logNotifyTrigger).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('when a non-empty #engine.state.triggers.notifications is updated with the same array', () => { | ||
const listener = vi.fn(); | ||
beforeEach(() => { | ||
engine = buildMockSearchEngine(createMockState()); | ||
setEngineTriggersState(['hello', 'world']); | ||
initNotifyTrigger(); | ||
notifyTrigger.subscribe(listener); | ||
setEngineTriggersState(['hello', 'world']); | ||
const [firstListener] = registeredListeners(); | ||
firstListener(); | ||
}); | ||
|
||
it('it does not call the listener', () => { | ||
expect(listener).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('it does not dispatch #logNotifyTrigger', () => { | ||
expect(logNotifyTrigger).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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
33 changes: 33 additions & 0 deletions
33
packages/headless/src/controllers/insight/triggers/headless-insight-notify-trigger.test.ts
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,33 @@ | ||
import { | ||
buildMockInsightEngine, | ||
MockedInsightEngine, | ||
} from '../../../test/mock-engine-v2.js'; | ||
import {buildMockInsightState} from '../../../test/mock-insight-state.js'; | ||
import { | ||
NotifyTrigger, | ||
buildNotifyTrigger, | ||
} from './headless-insight-notify-trigger.js'; | ||
|
||
vi.mock('../../../features/insight-search/insight-search-actions'); | ||
|
||
describe('NotifyTrigger', () => { | ||
let engine: MockedInsightEngine; | ||
let notifyTrigger: NotifyTrigger; | ||
|
||
function initNotifyTrigger() { | ||
notifyTrigger = buildNotifyTrigger(engine); | ||
} | ||
|
||
beforeEach(() => { | ||
engine = buildMockInsightEngine(buildMockInsightState()); | ||
initNotifyTrigger(); | ||
}); | ||
|
||
it('initializes', () => { | ||
expect(notifyTrigger).toBeTruthy(); | ||
}); | ||
|
||
it('exposes a #subscribe method', () => { | ||
expect(notifyTrigger.subscribe).toBeTruthy(); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
packages/headless/src/controllers/insight/triggers/headless-insight-notify-trigger.ts
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,23 @@ | ||
import {InsightEngine} from '../../../app/insight-engine/insight-engine.js'; | ||
import {logNotifyTrigger} from '../../../features/triggers/insight-trigger-analytics-actions.js'; | ||
import { | ||
buildCoreNotifyTrigger, | ||
NotifyTrigger, | ||
NotifyTriggerState, | ||
} from '../../core/triggers/headless-core-notify-trigger.js'; | ||
|
||
export type {NotifyTrigger, NotifyTriggerState}; | ||
|
||
/** | ||
* Creates an insight `NotifyTrigger` controller instance. | ||
* | ||
* @param engine - The insight engine. | ||
* @returns A `NotifyTrigger` controller instance. | ||
* */ | ||
export function buildNotifyTrigger(engine: InsightEngine): NotifyTrigger { | ||
return buildCoreNotifyTrigger(engine, { | ||
options: { | ||
logNotifyTriggerActionCreator: logNotifyTrigger, | ||
}, | ||
}); | ||
} |
Oops, something went wrong.