|
| 1 | +/* |
| 2 | +Copyright 2019 Adobe. All rights reserved. |
| 3 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +
|
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | +governing permissions and limitations under the License. |
| 11 | +*/ |
| 12 | +/* global console, window, dataLayer, CustomEvent */ |
| 13 | +(function() { |
| 14 | + 'use strict'; |
| 15 | + |
| 16 | + /* eslint no-console: "off" */ |
| 17 | + /* eslint no-unused-vars: "off" */ |
| 18 | + |
| 19 | + // Test case: scope = future -> console output should be: event3, event4 |
| 20 | + |
| 21 | + window.adobeDataLayer = window.adobeDataLayer || []; |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +// Example 1. If event listener has path filter then we return before / after value not the whole before / after state |
| 26 | + |
| 27 | +adobeDataLayer.addEventListener("test1", function(event, before, after) { |
| 28 | + console.log('LOG1: ', before, after); |
| 29 | +}, { path: 'component.image' }); |
| 30 | +adobeDataLayer.push({ |
| 31 | + event: "test1", |
| 32 | + component: { image: { id: 'image1' } } |
| 33 | +}); |
| 34 | +// LOG1: undefined, { id: "image1' } |
| 35 | + |
| 36 | +// Clear DL before next example |
| 37 | +adobeDataLayer.push({ component: null }); |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +// Example 2. If we add event listener and only then push items to DL we get before / after state arguments in callback function. |
| 42 | + |
| 43 | +adobeDataLayer.addEventListener("test2", function(event, before, after) { |
| 44 | + console.log('LOG2: ', before, after); |
| 45 | +}); |
| 46 | +adobeDataLayer.push({ |
| 47 | + event: "test2", |
| 48 | + count: "one" |
| 49 | +}); |
| 50 | +// LOG2: {}, { count: "one" } |
| 51 | + |
| 52 | +// Clear DL before next example |
| 53 | +adobeDataLayer.push({ count: null }); |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +// Example 3. If we add event listener then for all past items (items in DL) we will not get before / after arguments in callback function. |
| 58 | + |
| 59 | +adobeDataLayer.push({ |
| 60 | + event: "test3", |
| 61 | + count: "one" |
| 62 | +}); |
| 63 | +adobeDataLayer.addEventListener("test3", function(event, before, after) { |
| 64 | + console.log('LOG3: ', before, after); |
| 65 | +}); |
| 66 | +// LOG3: undefined, undefined |
| 67 | + |
| 68 | +// Clear DL before next example |
| 69 | +adobeDataLayer.push({ count: null }); |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +// Example 4. If we have data in DL, then add event listener and then push items to DL we will get before / after state arguments in callback function only for items that are pushed after event listener was added. |
| 74 | + |
| 75 | +adobeDataLayer.push({ |
| 76 | + event: "test4", |
| 77 | + count: "one" |
| 78 | +}); |
| 79 | +adobeDataLayer.addEventListener("test4", function(event, before, after) { |
| 80 | + console.log('LOG4: ', before, after); |
| 81 | +}); |
| 82 | +adobeDataLayer.push({ |
| 83 | + event: "test4", |
| 84 | + count: "two" |
| 85 | +}); |
| 86 | +// LOG4: undefined, undefined |
| 87 | +// LOG4: {}, { count: "one" } |
| 88 | + |
| 89 | +// Clear DL before next example |
| 90 | +adobeDataLayer.push({ count: null }); |
| 91 | + |
| 92 | +})(); |
0 commit comments