Skip to content
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

Add suppressToast option to intervention api #3541

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions demos/public/prod/enterprise/enterprise-funnel.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"isPartOf": {
"@type": ["CreativeWork", "Product"],
"name" : "Funnel for Enterprise Demo",
"productID": "CAowtIuHAQ:openaccess"
"productID": "CAowuv3XCw:openaccess"
}
}
</script>
Expand Down Expand Up @@ -74,9 +74,12 @@
row.innerHTML = html;
const button = row.getElementsByTagName('button')[0];
button.onclick = () => {
const isClosable = document.getElementById("isClosable").checked;
const suppressToast = document.getElementById("suppressToast").checked;
intervention.show({
isClosable: 'true',
onResult: addResultRow
isClosable,
onResult: addResultRow,
suppressToast,
});
}
table.appendChild(row);
Expand Down Expand Up @@ -105,6 +108,13 @@
<body>
<h1>Funnel for Enterprise Demo</h1>
<button onclick="clearLocalStorage()">Clear local storage</button>
<input type="checkbox" id="isClosable" name="scales" checked />
<label for="isClosable">isClosable</label>
</input>
<input type="checkbox" id="suppressToast" name="scales" checked />
<label for="suppressToast">suppressToast</label>
</input>
</div>
<article>
<table>
<caption>Available Interventions</caption>
Expand Down
15 changes: 7 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"@storybook/html": "7.6.19",
"@storybook/manager-webpack5": "6.5.16",
"cheerio": "1.0.0-rc.12",
"chromedriver": "125.0.3",
"chromedriver": "127.0.2",
"cookie-parser": "1.4.6",
"hogan-express": "0.5.2",
"is-running": "2.1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/api/available-intervention-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describes.realWin('AvailableIntervention', (env) => {

await availableIntervention.show({
isClosable: true,
suppressToast: true,
});

expect(actionFlowStub).to.have.been.calledWith(deps, {
Expand All @@ -49,6 +50,7 @@ describes.realWin('AvailableIntervention', (env) => {
configurationId: 'TEST_CONFIGURATION_ID',
onResult: undefined,
calledManually: true,
suppressToast: true,
});
expect(startStub).to.have.been.calledOnce;
});
Expand Down
4 changes: 4 additions & 0 deletions src/api/available-intervention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export interface ShowInterventionParams {
// Callback to get the result data from the intervention. Return a boolean
// indicating if the data was recorded successfully.
onResult?: (result: InterventionResult) => Promise<boolean> | boolean;

// Suppresses the completion toasts of the intervention
suppressToast?: boolean;
}

export class AvailableIntervention {
Expand Down Expand Up @@ -84,6 +87,7 @@ export class AvailableIntervention {
configurationId: this.intervention.configurationId,
onResult: params.onResult,
calledManually: true,
suppressToast: params.suppressToast,
});
return flow.start();
}
Expand Down
37 changes: 37 additions & 0 deletions src/runtime/audience-action-flow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,43 @@ describes.realWin('AudienceActionIframeFlow', (env) => {
onResultMock.verify();
});

it(`suppresses toasts`, async () => {
const audienceActionFlow = new AudienceActionIframeFlow(runtime, {
action: 'TYPE_NEWSLETTER_SIGNUP',
configurationId: 'configId',
onCancel: onCancelSpy,
autoPromptType: AutoPromptType.SUBSCRIPTION,
calledManually: false,
suppressToast: true,
});
activitiesMock.expects('openIframe').resolves(port);
entitlementsManagerMock.expects('clear').once();
entitlementsManagerMock.expects('getEntitlements').once();
storageMock
.expects('set')
.withExactArgs(Constants.USER_TOKEN, 'fake user token', true)
.exactly(1);
storageMock
.expects('set')
.withExactArgs(Constants.READ_TIME, EXPECTED_TIME_STRING, false)
.exactly(1);

const toastOpenStub = sandbox.stub(Toast.prototype, 'open');

await audienceActionFlow.start();
const completeAudienceActionResponse = new CompleteAudienceActionResponse();
completeAudienceActionResponse.setActionCompleted(false);
completeAudienceActionResponse.setAlreadyCompleted(true);
completeAudienceActionResponse.setSwgUserToken('fake user token');
completeAudienceActionResponse.setUserEmail('xxx@gmail.com');
const messageCallback = messageMap[completeAudienceActionResponse.label()];
messageCallback(completeAudienceActionResponse);

entitlementsManagerMock.verify();
storageMock.verify();
expect(toastOpenStub).not.to.be.called;
});

it('should trigger login flow for a registered user', async () => {
const loginStub = sandbox.stub(runtime.callbacks(), 'triggerLoginRequest');
const audienceActionFlow = new AudienceActionIframeFlow(runtime, {
Expand Down
15 changes: 9 additions & 6 deletions src/runtime/audience-action-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface AudienceActionIframeParams {
isClosable?: boolean;
calledManually: boolean;
shouldRenderPreview?: boolean;
suppressToast?: boolean;
}

// TODO: mhkawano - replace these consts in the project with these
Expand Down Expand Up @@ -211,12 +212,14 @@ export class AudienceActionIframeFlow implements AudienceActionFlow {
});
}

if (response.getActionCompleted()) {
this.showSignedInToast_(response.getUserEmail() ?? '');
} else if (response.getAlreadyCompleted()) {
this.showAlreadyOptedInToast_();
} else {
this.showFailedOptedInToast_();
if (!this.params_.suppressToast) {
if (response.getActionCompleted()) {
this.showSignedInToast_(response.getUserEmail() ?? '');
} else if (response.getAlreadyCompleted()) {
this.showAlreadyOptedInToast_();
} else {
this.showFailedOptedInToast_();
}
}
const now = Date.now().toString();
this.deps_
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/entitlements-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3113,6 +3113,7 @@ describes.realWin('EntitlementsManager', (env) => {

availableIntervention.show({
isClosable: true,
suppressToast: true,
});

expect(actionFlowSpy).to.have.been.calledWith(deps, {
Expand All @@ -3121,6 +3122,7 @@ describes.realWin('EntitlementsManager', (env) => {
configurationId: 'TEST_CONFIGURATION_ID',
onResult: undefined,
calledManually: true,
suppressToast: true,
});
expect(startSpy).to.have.been.calledOnce;
});
Expand Down
Loading