|
| 1 | +import { Selector } from 'testcafe'; |
| 2 | +import { REQUEST_BODY } from '../test-utils/integ-test-utils'; |
| 3 | + |
| 4 | +const dispatch: Selector = Selector(`#dispatch`); |
| 5 | +const recordEventAPI: Selector = Selector(`#recordEventAPI`); |
| 6 | +const recordEventApiEmptyEvent: Selector = Selector(`#recordEventAPIEmpty`); |
| 7 | +const pluginRecord: Selector = Selector(`#pluginRecord`); |
| 8 | +const pluginRecordEmptyEvent: Selector = Selector(`#pluginRecordEmpty`); |
| 9 | + |
| 10 | +const API_EVENT_TYPE = 'custom_event_api'; |
| 11 | +const PLUGIN_EVENT_TYPE = 'custom_event_plugin'; |
| 12 | +const COUNT = 5; |
| 13 | + |
| 14 | +fixture('Custom Events API & Plugin').page( |
| 15 | + 'http://localhost:8080/custom_event.html' |
| 16 | +); |
| 17 | + |
| 18 | +const removeUnwantedEvents = (json: any) => { |
| 19 | + const newEventsList = json.RumEvents.filter( |
| 20 | + (e) => |
| 21 | + /(custom_event_api)/.test(e.type) || |
| 22 | + /(custom_event_plugin)/.test(e.type) |
| 23 | + ); |
| 24 | + |
| 25 | + json.RumEvents = newEventsList; |
| 26 | + return json; |
| 27 | +}; |
| 28 | + |
| 29 | +test('when a recordEvent API is called then event is recorded', async (t: TestController) => { |
| 30 | + // If we click too soon, the client/event collector plugin will not be loaded and will not record the click. |
| 31 | + // This could be a symptom of an issue with RUM web client load speed, or prioritization of script execution. |
| 32 | + await t |
| 33 | + .wait(300) |
| 34 | + .click(recordEventAPI) |
| 35 | + .click(dispatch) |
| 36 | + .expect(REQUEST_BODY.textContent) |
| 37 | + .contains('BatchId'); |
| 38 | + |
| 39 | + const json = removeUnwantedEvents( |
| 40 | + JSON.parse(await REQUEST_BODY.textContent) |
| 41 | + ); |
| 42 | + const eventType = json.RumEvents[0].type; |
| 43 | + const eventDetails = JSON.parse(json.RumEvents[0].details); |
| 44 | + |
| 45 | + await t |
| 46 | + .expect(eventType) |
| 47 | + .eql(API_EVENT_TYPE) |
| 48 | + .expect(eventDetails.customEventVersion) |
| 49 | + .eql(255); |
| 50 | +}); |
| 51 | + |
| 52 | +test('when a recordEvent API is called x times then event is recorded x times', async (t: TestController) => { |
| 53 | + // If we click too soon, the client/event collector plugin will not be loaded and will not record the click. |
| 54 | + // This could be a symptom of an issue with RUM web client load speed, or prioritization of script execution. |
| 55 | + // Record event 5 times. |
| 56 | + await t.wait(300); |
| 57 | + for (let i = 0; i < COUNT; i++) { |
| 58 | + await t.click(recordEventAPI); |
| 59 | + } |
| 60 | + |
| 61 | + await t |
| 62 | + .click(dispatch) |
| 63 | + .expect(REQUEST_BODY.textContent) |
| 64 | + .contains('BatchId'); |
| 65 | + |
| 66 | + const json = removeUnwantedEvents( |
| 67 | + JSON.parse(await REQUEST_BODY.textContent) |
| 68 | + ); |
| 69 | + |
| 70 | + await t.expect(json.RumEvents.length).eql(COUNT); |
| 71 | + json.RumEvents.forEach(async (item) => { |
| 72 | + const eventType = item.type; |
| 73 | + const eventDetails = JSON.parse(item.details); |
| 74 | + await t |
| 75 | + .expect(eventType) |
| 76 | + .eql(API_EVENT_TYPE) |
| 77 | + .expect(eventDetails.customEventVersion) |
| 78 | + .eql(255); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +test('when a recordEvent API has empty event_data then RumEvent detail is empty', async (t: TestController) => { |
| 83 | + await t |
| 84 | + .wait(300) |
| 85 | + .click(recordEventApiEmptyEvent) |
| 86 | + .click(dispatch) |
| 87 | + .expect(REQUEST_BODY.textContent) |
| 88 | + .contains('BatchId'); |
| 89 | + |
| 90 | + const json = removeUnwantedEvents( |
| 91 | + JSON.parse(await REQUEST_BODY.textContent) |
| 92 | + ); |
| 93 | + await t |
| 94 | + .expect(json.RumEvents.length) |
| 95 | + .eql(1) |
| 96 | + .expect(json.RumEvents[0].type) |
| 97 | + .eql(API_EVENT_TYPE) |
| 98 | + .expect(json.RumEvents[0].details) |
| 99 | + .eql('{}'); |
| 100 | +}); |
| 101 | + |
| 102 | +test('when a plugin calls recordEvent then the event is recorded', async (t: TestController) => { |
| 103 | + // If we click too soon, the client/event collector plugin will not be loaded and will not record the click. |
| 104 | + // This could be a symptom of an issue with RUM web client load speed, or prioritization of script execution. |
| 105 | + await t |
| 106 | + .wait(300) |
| 107 | + .click(pluginRecord) |
| 108 | + .click(dispatch) |
| 109 | + .expect(REQUEST_BODY.textContent) |
| 110 | + .contains('BatchId'); |
| 111 | + |
| 112 | + const json = removeUnwantedEvents( |
| 113 | + JSON.parse(await REQUEST_BODY.textContent) |
| 114 | + ); |
| 115 | + const eventType = json.RumEvents[0].type; |
| 116 | + const eventDetails = JSON.parse(json.RumEvents[0].details); |
| 117 | + |
| 118 | + await t |
| 119 | + .expect(eventType) |
| 120 | + .eql(PLUGIN_EVENT_TYPE) |
| 121 | + .expect(eventDetails.intField) |
| 122 | + .eql(1) |
| 123 | + .expect(eventDetails.stringField) |
| 124 | + .eql('string') |
| 125 | + .expect(eventDetails.nestedField) |
| 126 | + .eql({ subfield: 1 }); |
| 127 | +}); |
| 128 | + |
| 129 | +test('when a plugin calls recordEvent x times then event is recorded x times', async (t: TestController) => { |
| 130 | + // If we click too soon, the client/event collector plugin will not be loaded and will not record the click. |
| 131 | + // This could be a symptom of an issue with RUM web client load speed, or prioritization of script execution. |
| 132 | + // Record event 5 times. |
| 133 | + await t.wait(300); |
| 134 | + for (let i = 0; i < COUNT; i++) { |
| 135 | + await t.click(pluginRecord); |
| 136 | + } |
| 137 | + await t |
| 138 | + .click(dispatch) |
| 139 | + .expect(REQUEST_BODY.textContent) |
| 140 | + .contains('BatchId'); |
| 141 | + |
| 142 | + const json = removeUnwantedEvents( |
| 143 | + JSON.parse(await REQUEST_BODY.textContent) |
| 144 | + ); |
| 145 | + |
| 146 | + await t.expect(json.RumEvents.length).eql(COUNT); |
| 147 | + json.RumEvents.forEach(async (item) => { |
| 148 | + const eventType = item.type; |
| 149 | + const eventDetails = JSON.parse(item.details); |
| 150 | + await t |
| 151 | + .expect(eventType) |
| 152 | + .eql(PLUGIN_EVENT_TYPE) |
| 153 | + .expect(eventDetails.intField) |
| 154 | + .eql(1) |
| 155 | + .expect(eventDetails.stringField) |
| 156 | + .eql('string') |
| 157 | + .expect(eventDetails.nestedField) |
| 158 | + .eql({ subfield: 1 }); |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +test('when plugin recordEvent has empty event_data then RumEvent details is empty', async (t: TestController) => { |
| 163 | + // If we click too soon, the client/event collector plugin will not be loaded and will not record the click. |
| 164 | + // This could be a symptom of an issue with RUM web client load speed, or prioritization of script execution. |
| 165 | + await t.wait(300); |
| 166 | + |
| 167 | + await t |
| 168 | + .click(pluginRecordEmptyEvent) |
| 169 | + .click(dispatch) |
| 170 | + .expect(REQUEST_BODY.textContent) |
| 171 | + .contains('BatchId'); |
| 172 | + |
| 173 | + const json = removeUnwantedEvents( |
| 174 | + JSON.parse(await REQUEST_BODY.textContent) |
| 175 | + ); |
| 176 | + await t |
| 177 | + .expect(json.RumEvents.length) |
| 178 | + .eql(1) |
| 179 | + .expect(json.RumEvents[0].type) |
| 180 | + .eql(PLUGIN_EVENT_TYPE) |
| 181 | + .expect(json.RumEvents[0].details) |
| 182 | + .eql('{}'); |
| 183 | +}); |
0 commit comments