Repository navigation
fix: dispatch apple pay session events in dc #288
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
Open
jakubjasinsky
wants to merge
2
commits into
master
Choose a base branch
from
jakub-apple-pay-session-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,144 @@ | ||
| import { describe, expect, it, vi } from "vitest" | ||
| import { loadApplePay } from "../support/loadNamespace" | ||
|
|
||
| class FakeApplePaySession { | ||
| onvalidatemerchant: (event: { validationURL: string }) => void | ||
| completeMerchantValidation = vi.fn() | ||
| abort = vi.fn() | ||
| begin = vi.fn() | ||
| } | ||
|
|
||
| type Respond = (onSuccess: Function, onError: Function) => void | ||
|
|
||
| const merchantValidated: Respond = onSuccess => | ||
| onSuccess({ success: true, session_payload: { id: "merchant_session" } }) | ||
| const merchantRejected: Respond = onSuccess => | ||
| onSuccess({ success: false, error_code: "bad.domain", message: "Bad domain" }) | ||
| const requestFailed: Respond = (_onSuccess, onError) => | ||
| onError({}, {}, "processout-js.network-issue") | ||
|
|
||
| function setup(respond: Respond, configure: (applePay: any, calls: string[]) => void = () => {}) { | ||
| const ProcessOut = loadApplePay(FakeApplePaySession) | ||
| const instance = { | ||
| endpoint: () => "https://api.test/applepay/sessions", | ||
| apiRequest: ( | ||
| _method: string, | ||
| _url: string, | ||
| _body: unknown, | ||
| onSuccess: Function, | ||
| onError: Function, | ||
| ) => respond(onSuccess, onError), | ||
| } | ||
| const calls: string[] = [] | ||
| const newSessionOnSuccess = vi.fn() | ||
| const newSessionOnError = vi.fn() | ||
| const onTokenizeError = vi.fn(() => calls.push("tokenize_error")) | ||
|
|
||
| const applePay = new ProcessOut.ApplePayWrapper(instance).newSession( | ||
| { total: { label: "Order", amount: "10.00" } }, | ||
| newSessionOnSuccess, | ||
| newSessionOnError, | ||
| ) | ||
| configure(applePay, calls) | ||
| applePay.tokenize({}, vi.fn(), onTokenizeError) | ||
|
|
||
| const session: FakeApplePaySession = applePay.getSession() | ||
| const validateMerchant = () => | ||
| session.onvalidatemerchant({ validationURL: "https://apple.test/validate" }) | ||
|
|
||
| return { | ||
| calls, | ||
| session, | ||
| validateMerchant, | ||
| newSessionOnSuccess, | ||
| newSessionOnError, | ||
| onTokenizeError, | ||
| } | ||
| } | ||
|
|
||
| describe("existing behaviour (no session handlers set)", () => { | ||
| it("completes merchant validation without calling any handler", () => { | ||
| const { calls, session, validateMerchant } = setup(merchantValidated) | ||
|
|
||
| validateMerchant() | ||
|
|
||
| expect(session.completeMerchantValidation).toHaveBeenCalledWith({ id: "merchant_session" }) | ||
| expect(session.abort).not.toHaveBeenCalled() | ||
| expect(calls).toEqual([]) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ["the API rejects the merchant", merchantRejected, "bad.domain"], | ||
| ["the request fails", requestFailed, "processout-js.network-issue"], | ||
| ])("reports to the tokenize error handler and aborts when %s", (_name, respond, code) => { | ||
| const { session, validateMerchant, onTokenizeError } = setup(respond) | ||
|
|
||
| validateMerchant() | ||
|
|
||
| expect(onTokenizeError).toHaveBeenCalledOnce() | ||
| expect(onTokenizeError.mock.calls[0][0].code).toBe(code) | ||
| expect(session.completeMerchantValidation).not.toHaveBeenCalled() | ||
| expect(session.abort).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it.each([ | ||
| ["succeeds", merchantValidated], | ||
| ["fails", merchantRejected], | ||
| ])("never calls the callbacks given to newSession when validation %s", (_name, respond) => { | ||
| const { validateMerchant, newSessionOnSuccess, newSessionOnError } = setup(respond) | ||
|
|
||
| validateMerchant() | ||
|
|
||
| expect(newSessionOnSuccess).not.toHaveBeenCalled() | ||
| expect(newSessionOnError).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe("session handlers", () => { | ||
| it("calls onmerchantvalidated after the merchant validation is completed", () => { | ||
| const { calls, session, validateMerchant } = setup(merchantValidated, (applePay, calls) => { | ||
| applePay.onmerchantvalidated = () => { | ||
| expect(session.completeMerchantValidation).toHaveBeenCalledOnce() | ||
| calls.push("merchant_validated") | ||
| } | ||
| }) | ||
|
|
||
| validateMerchant() | ||
|
|
||
| expect(calls).toEqual(["merchant_validated"]) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ["the API rejects the merchant", merchantRejected, "bad.domain"], | ||
| ["the request fails", requestFailed, "processout-js.network-issue"], | ||
| ])( | ||
| "calls onmerchantvalidationerror before the tokenize error handler when %s", | ||
| (_name, respond, code) => { | ||
| const onMerchantValidationError = vi.fn() | ||
| const { calls, session, validateMerchant } = setup(respond, (applePay, calls) => { | ||
| applePay.onmerchantvalidationerror = (err: unknown) => { | ||
| onMerchantValidationError(err) | ||
| calls.push("merchant_validation_error") | ||
| } | ||
| }) | ||
|
|
||
| validateMerchant() | ||
|
|
||
| expect(calls).toEqual(["merchant_validation_error", "tokenize_error"]) | ||
| expect(onMerchantValidationError.mock.calls[0][0].code).toBe(code) | ||
| expect(session.abort).toHaveBeenCalledOnce() | ||
| }, | ||
| ) | ||
|
|
||
| it("still reports the error and aborts when onmerchantvalidationerror throws", () => { | ||
| const { session, validateMerchant, onTokenizeError } = setup(merchantRejected, applePay => { | ||
| applePay.onmerchantvalidationerror = () => { | ||
| throw new Error("listener blew up") | ||
| } | ||
| }) | ||
|
|
||
| expect(validateMerchant).toThrow("listener blew up") | ||
| expect(onTokenizeError).toHaveBeenCalledOnce() | ||
| expect(session.abort).toHaveBeenCalledOnce() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
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.
Does it make sense to add
@deprecatedas well?As far as I understand, we cannot just remove them, because this constructor function may be used by the external callers directly (i.e. merchants)?