1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var http_1 = require("../../commons/utils/http");
|
5 | var event_1 = require("../../event/core/event");
|
6 | var vtex_runner_1 = require("./vtex-runner");
|
7 | var vtexRunner = new vtex_runner_1.VTEXRunner();
|
8 | beforeEach(function () {
|
9 | var post = jest.fn().mockReturnValue({ status: 204 });
|
10 | post.mockClear();
|
11 | http_1.Http.post = post;
|
12 | });
|
13 | test("running the vtex runner on a confirmation page should trigger order confirmation event", function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
14 | return tslib_1.__generator(this, function (_a) {
|
15 | switch (_a.label) {
|
16 | case 0:
|
17 | window.dataLayer = [
|
18 | {
|
19 | event: "orderPlaced",
|
20 | transactionId: "orderId",
|
21 | transactionProducts: [
|
22 | {
|
23 | id: "productId",
|
24 | quantity: 1,
|
25 | price: 99.99,
|
26 | },
|
27 | ],
|
28 | },
|
29 | ];
|
30 | vtexRunner.start();
|
31 |
|
32 | return [4 , new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
|
33 | case 1:
|
34 |
|
35 | _a.sent();
|
36 | expect(http_1.Http.post).toHaveBeenCalledTimes(2);
|
37 | expect(http_1.Http.post).toHaveBeenNthCalledWith(1, expect.any(String), expect.objectContaining({ type: event_1.EventType.SessionPing }));
|
38 | expect(http_1.Http.post).toHaveBeenNthCalledWith(2, expect.any(String), expect.objectContaining({ type: event_1.EventType.PageConfirmation, products: expect.any(Array) }));
|
39 | return [2 ];
|
40 | }
|
41 | });
|
42 | }); });
|
43 | test("running the vtex runner on a cart page should trigger cart events", function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
44 | return tslib_1.__generator(this, function (_a) {
|
45 | switch (_a.label) {
|
46 | case 0:
|
47 | window.dataLayer = [
|
48 | {
|
49 | event: "cart",
|
50 | ecommerce: {
|
51 | checkout: {
|
52 | products: [
|
53 | {
|
54 | id: "111",
|
55 | quantity: 1,
|
56 | },
|
57 | {
|
58 | id: "222",
|
59 | quantity: 2,
|
60 | },
|
61 | ],
|
62 | },
|
63 | },
|
64 | },
|
65 | ];
|
66 | return [4 , vtexRunner.start()];
|
67 | case 1:
|
68 | _a.sent();
|
69 | window.dataLayer.push({
|
70 | event: "removeFromCart",
|
71 | ecommerce: {
|
72 | remove: {
|
73 | products: [
|
74 | {
|
75 | id: "222",
|
76 | quantity: 1,
|
77 | },
|
78 | ],
|
79 | },
|
80 | },
|
81 | });
|
82 |
|
83 | return [4 , new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
|
84 | case 2:
|
85 |
|
86 | _a.sent();
|
87 | expect(http_1.Http.post).toHaveBeenCalledTimes(3);
|
88 | expect(http_1.Http.post).toHaveBeenNthCalledWith(1, expect.any(String), expect.objectContaining({ type: event_1.EventType.SessionPing }));
|
89 | expect(http_1.Http.post).toHaveBeenNthCalledWith(2, expect.any(String), expect.objectContaining({ type: event_1.EventType.PageCart, products: expect.any(Array) }));
|
90 | expect(http_1.Http.post).toHaveBeenNthCalledWith(3, expect.any(String), expect.objectContaining({ type: event_1.EventType.SessionCartRemove, products: expect.any(Array) }));
|
91 | return [2 ];
|
92 | }
|
93 | });
|
94 | }); });
|
95 | test("should not send an event if no important dataLayer events were found", function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
96 | return tslib_1.__generator(this, function (_a) {
|
97 | switch (_a.label) {
|
98 | case 0:
|
99 | window.dataLayer = [];
|
100 | return [4 , vtexRunner.start()];
|
101 | case 1:
|
102 | _a.sent();
|
103 | expect(http_1.Http.post).toHaveBeenCalledTimes(1);
|
104 | expect(http_1.Http.post).toHaveBeenNthCalledWith(1, expect.any(String), expect.objectContaining({ type: event_1.EventType.SessionPing }));
|
105 | return [2 ];
|
106 | }
|
107 | });
|
108 | }); });
|
109 |
|
\ | No newline at end of file |