|
1 | 1 | import * as loader from '../../../lib/load-script'
|
2 |
| -import { remoteLoader } from '..' |
| 2 | +import { ActionDestination, remoteLoader } from '..' |
3 | 3 | import { AnalyticsBrowser, LegacySettings } from '../../../browser'
|
4 | 4 | import { InitOptions } from '../../../core/analytics'
|
5 | 5 | import { Context } from '../../../core/context'
|
@@ -597,4 +597,105 @@ describe('Remote Loader', () => {
|
597 | 597 | }
|
598 | 598 | `)
|
599 | 599 | })
|
| 600 | + |
| 601 | + it('only applies destination middleware to destination actions', async () => { |
| 602 | + const validPlugin = { |
| 603 | + name: 'valid', |
| 604 | + version: '1.0.0', |
| 605 | + type: 'enrichment', |
| 606 | + load: () => {}, |
| 607 | + isLoaded: () => true, |
| 608 | + track: (ctx: Context) => ctx, |
| 609 | + } |
| 610 | + |
| 611 | + const cdnSettings: LegacySettings = { |
| 612 | + integrations: {}, |
| 613 | + middlewareSettings: { |
| 614 | + routingRules: [ |
| 615 | + { |
| 616 | + matchers: [ |
| 617 | + { |
| 618 | + ir: '["=","event",{"value":"Item Impression"}]', |
| 619 | + type: 'fql', |
| 620 | + }, |
| 621 | + ], |
| 622 | + scope: 'destinations', |
| 623 | + target_type: 'workspace::project::destination::config', |
| 624 | + transformers: [[{ type: 'drop' }]], |
| 625 | + destinationName: 'oldValidName', |
| 626 | + }, |
| 627 | + ], |
| 628 | + }, |
| 629 | + remotePlugins: [ |
| 630 | + { |
| 631 | + name: 'valid', |
| 632 | + creationName: 'oldValidName', |
| 633 | + url: 'valid', |
| 634 | + libraryName: 'valid', |
| 635 | + settings: { foo: true }, |
| 636 | + }, |
| 637 | + ], |
| 638 | + } |
| 639 | + |
| 640 | + // @ts-expect-error not gonna return a script tag sorry |
| 641 | + jest.spyOn(loader, 'loadScript').mockImplementation((url: string) => { |
| 642 | + if (url === 'valid') { |
| 643 | + window['valid'] = jest.fn().mockImplementation(() => validPlugin) |
| 644 | + } |
| 645 | + |
| 646 | + return Promise.resolve(true) |
| 647 | + }) |
| 648 | + |
| 649 | + const middleware = jest.fn().mockImplementation(() => true) |
| 650 | + |
| 651 | + const plugins = await remoteLoader(cdnSettings, {}, {}, false, middleware) |
| 652 | + const plugin = plugins[0] as ActionDestination |
| 653 | + plugin.addMiddleware(middleware) |
| 654 | + await plugin.track(new Context({ type: 'track' })) |
| 655 | + expect(middleware).not.toHaveBeenCalled() |
| 656 | + }) |
| 657 | + |
| 658 | + it('non destination type plugins can modify the context', async () => { |
| 659 | + const validPlugin = { |
| 660 | + name: 'valid', |
| 661 | + version: '1.0.0', |
| 662 | + type: 'enrichment', |
| 663 | + load: () => {}, |
| 664 | + isLoaded: () => true, |
| 665 | + track: (ctx: Context) => { |
| 666 | + ctx.event.name += 'bar' |
| 667 | + return ctx |
| 668 | + }, |
| 669 | + } |
| 670 | + |
| 671 | + const cdnSettings: LegacySettings = { |
| 672 | + integrations: {}, |
| 673 | + remotePlugins: [ |
| 674 | + { |
| 675 | + name: 'valid', |
| 676 | + creationName: 'valid', |
| 677 | + url: 'valid', |
| 678 | + libraryName: 'valid', |
| 679 | + settings: { foo: true }, |
| 680 | + }, |
| 681 | + ], |
| 682 | + } |
| 683 | + |
| 684 | + // @ts-expect-error not gonna return a script tag sorry |
| 685 | + jest.spyOn(loader, 'loadScript').mockImplementation((url: string) => { |
| 686 | + if (url === 'valid') { |
| 687 | + window['valid'] = jest.fn().mockImplementation(() => validPlugin) |
| 688 | + } |
| 689 | + |
| 690 | + return Promise.resolve(true) |
| 691 | + }) |
| 692 | + |
| 693 | + const plugins = await remoteLoader(cdnSettings, {}, {}, false) |
| 694 | + const plugin = plugins[0] as ActionDestination |
| 695 | + const newCtx = await plugin.track( |
| 696 | + new Context({ type: 'track', name: 'foo' }) |
| 697 | + ) |
| 698 | + |
| 699 | + expect(newCtx.event.name).toEqual('foobar') |
| 700 | + }) |
600 | 701 | })
|
0 commit comments