UNPKG

5.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var http_1 = require("../../commons/utils/http");
5var event_1 = require("../../event/core/event");
6var vtex_runner_1 = require("./vtex-runner");
7var vtexRunner = new vtex_runner_1.VTEXRunner();
8beforeEach(function () {
9 var post = jest.fn().mockReturnValue({ status: 204 });
10 post.mockClear();
11 http_1.Http.post = post;
12});
13test("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 // Wait for all events to be processed.
32 return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
33 case 1:
34 // Wait for all events to be processed.
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 /*return*/];
40 }
41 });
42}); });
43test("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 /*yield*/, 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 // Wait for all events to be processed.
83 return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
84 case 2:
85 // Wait for all events to be processed.
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 /*return*/];
92 }
93 });
94}); });
95test("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 /*yield*/, 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 /*return*/];
106 }
107 });
108}); });
109//# sourceMappingURL=vtex-runner.test.js.map
\No newline at end of file