import { __awaiter, __generator } from "tslib";
import { Analytics } from '@/analytics';
import { pageEnrichment, pageDefaults } from '..';
var ajs;
describe('Page Enrichment', function () {
    beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    ajs = new Analytics({
                        writeKey: 'abc_123',
                    });
                    return [4 /*yield*/, ajs.register(pageEnrichment)];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); });
    test('enriches page calls', function () { return __awaiter(void 0, void 0, void 0, function () {
        var ctx;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, ajs.page('Checkout', {})];
                case 1:
                    ctx = _a.sent();
                    expect(ctx.event.properties).toMatchInlineSnapshot("\n      Object {\n        \"name\": \"Checkout\",\n        \"path\": \"/\",\n        \"referrer\": \"\",\n        \"search\": \"\",\n        \"title\": \"\",\n        \"url\": \"http://localhost/\",\n      }\n    ");
                    return [2 /*return*/];
            }
        });
    }); });
    test('enriches track events with the page context', function () { return __awaiter(void 0, void 0, void 0, function () {
        var ctx;
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4 /*yield*/, ajs.track('My event', {
                        banana: 'phone',
                    })];
                case 1:
                    ctx = _b.sent();
                    expect((_a = ctx.event.context) === null || _a === void 0 ? void 0 : _a.page).toMatchInlineSnapshot("\n      Object {\n        \"path\": \"/\",\n        \"referrer\": \"\",\n        \"search\": \"\",\n        \"title\": \"\",\n        \"url\": \"http://localhost/\",\n      }\n    ");
                    return [2 /*return*/];
            }
        });
    }); });
    test('enriches page events with the page context', function () { return __awaiter(void 0, void 0, void 0, function () {
        var ctx;
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4 /*yield*/, ajs.page('My event', { banana: 'phone' }, { page: { url: 'not-localhost' } })];
                case 1:
                    ctx = _b.sent();
                    expect((_a = ctx.event.context) === null || _a === void 0 ? void 0 : _a.page).toMatchInlineSnapshot("\n    Object {\n      \"path\": \"/\",\n      \"referrer\": \"\",\n      \"search\": \"\",\n      \"title\": \"\",\n      \"url\": \"not-localhost\",\n    }\n  ");
                    return [2 /*return*/];
            }
        });
    }); });
    test('enriches identify events with the page context', function () { return __awaiter(void 0, void 0, void 0, function () {
        var ctx;
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4 /*yield*/, ajs.identify('Netto', {
                        banana: 'phone',
                    })];
                case 1:
                    ctx = _b.sent();
                    expect((_a = ctx.event.context) === null || _a === void 0 ? void 0 : _a.page).toMatchInlineSnapshot("\n      Object {\n        \"path\": \"/\",\n        \"referrer\": \"\",\n        \"search\": \"\",\n        \"title\": \"\",\n        \"url\": \"http://localhost/\",\n      }\n    ");
                    return [2 /*return*/];
            }
        });
    }); });
});
describe('pageDefaults', function () {
    var el = document.createElement('link');
    el.setAttribute('rel', 'canonical');
    beforeEach(function () {
        el.setAttribute('href', '');
        document.clear();
    });
    afterEach(function () {
        jest.restoreAllMocks();
    });
    it('handles no canonical links', function () {
        var defs = pageDefaults();
        expect(defs.url).not.toBeNull();
    });
    it('handles canonical links', function () {
        el.setAttribute('href', 'http://www.segment.local');
        document.body.appendChild(el);
        var defs = pageDefaults();
        expect(defs.url).toEqual('http://www.segment.local');
    });
    it('handles canonical links with a path', function () {
        el.setAttribute('href', 'http://www.segment.local/test');
        document.body.appendChild(el);
        var defs = pageDefaults();
        expect(defs.url).toEqual('http://www.segment.local/test');
        expect(defs.path).toEqual('/test');
    });
    it('handles canonical links with search params in the url', function () {
        el.setAttribute('href', 'http://www.segment.local?test=true');
        document.body.appendChild(el);
        var defs = pageDefaults();
        expect(defs.url).toEqual('http://www.segment.local?test=true');
    });
});
//# sourceMappingURL=index.test.js.map