forked from houdiniproject/houdini
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move postSuccess into DonationSubmitter
- Loading branch information
Showing
8 changed files
with
313 additions
and
58 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
81 changes: 81 additions & 0 deletions
81
client/js/nonprofits/donate/DonationSubmitter/PlausibleCallback.spec.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,81 @@ | ||
// License: LGPL-3.0-or-later | ||
import PlausibleCallback from './PlausibleCallback'; | ||
|
||
|
||
describe('PlausibleCallback', () => { | ||
describe('.canRun', () => { | ||
it('false when getPlausible is undefined', () => { | ||
const c = new PlausibleCallback({ props: {}} as any); | ||
expect(c.canRun()).toEqual(false) | ||
}) | ||
|
||
it('false when getPlausible returns undefined', () => { | ||
const c = new PlausibleCallback({ props: {getPlausible: ():any => undefined}} as any); | ||
expect(c.canRun()).toEqual(false) | ||
}) | ||
|
||
it('true when returns plausible function', () => { | ||
const realPlausibleFunc = jest.fn(); | ||
const c = new PlausibleCallback({ props: {getPlausible: ():any => realPlausibleFunc}} as any); | ||
expect(c.canRun()).toEqual(true); | ||
}) | ||
}) | ||
|
||
describe('.run', () => { | ||
function build(result?:{charge?:{amount?:number}}) { | ||
const realPlausibleFunc = jest.fn(); | ||
return { | ||
plausible: realPlausibleFunc, | ||
obj: new PlausibleCallback({ props: {getPlausible: ():any => realPlausibleFunc}, result} as any) | ||
}; | ||
|
||
} | ||
|
||
it('calls plausible with no amount when result is undefined', async () => { | ||
const {plausible, obj} = build(); | ||
await obj.run(); | ||
expect(plausible).toHaveBeenCalledWith('payment_succeeded', { | ||
props: { | ||
amount: undefined, | ||
} | ||
}); | ||
}) | ||
|
||
it('calls plausible with no amount when charge is undefined', async () => { | ||
const {plausible, obj} = build({}); | ||
await obj.run(); | ||
expect(plausible).toHaveBeenCalledWith('payment_succeeded', { | ||
props: { | ||
amount: undefined, | ||
} | ||
}); | ||
}) | ||
|
||
it('calls plausible with no amount when charge.amount is undefined', async () => { | ||
const {plausible, obj} = build({charge:{}}); | ||
await obj.run(); | ||
expect(plausible).toHaveBeenCalledWith('payment_succeeded', { | ||
props: { | ||
amount: undefined, | ||
} | ||
}); | ||
}) | ||
|
||
it('calls plausible with amount/100 when charge.amount is defined', async () => { | ||
const {plausible, obj} = build({charge:{amount: 1000}}); | ||
await obj.run(); | ||
expect(plausible).toHaveBeenCalledWith('payment_succeeded', { | ||
props: { | ||
amount: 10, | ||
} | ||
}); | ||
}) | ||
}); | ||
|
||
describe('.catchError', () => { | ||
it('does not rethrow errors', () => { | ||
const c = new PlausibleCallback({} as any); | ||
expect(() => c.catchError(new Error())).not.toThrow(); | ||
}) | ||
}) | ||
}); |
36 changes: 36 additions & 0 deletions
36
client/js/nonprofits/donate/DonationSubmitter/PlausibleCallback.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,36 @@ | ||
import { Callback } from "../../../../../app/javascript/common/Callbacks"; | ||
import DonationSubmitter from './'; | ||
|
||
// License: LGPL-3.0-or-later | ||
export interface PlausibleFunction { | ||
(eventType: string, val: any): void | ||
} | ||
|
||
export interface GetPlausible { | ||
|
||
(): PlausibleFunction | undefined | ||
} | ||
|
||
|
||
export default class PlausibleCallback extends Callback<DonationSubmitter> { | ||
|
||
private get plausibleFunction(): PlausibleFunction { | ||
return this.props.props.getPlausible() | ||
} | ||
canRun(): boolean { | ||
return !!(this.props.props.getPlausible && this.props.props.getPlausible()) | ||
} | ||
|
||
run(): void { | ||
this.plausibleFunction('payment_succeeded', { | ||
props: { | ||
amount: this.props.result?.charge?.amount && (this.props.result.charge.amount / 100) | ||
} | ||
}); | ||
} | ||
|
||
catchError(e: unknown): void { | ||
console.log(e); | ||
} | ||
|
||
} |
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
Oops, something went wrong.