import { __awaiter, __generator } from "tslib";
import assert from 'assert';
import { mocked } from 'ts-jest/utils';
import unfetch from 'unfetch';
import { segmentio } from '..';
import { Analytics } from '../../../analytics';
import { pageEnrichment } from '../../page-enrichment';
import cookie from 'js-cookie';
jest.mock('unfetch', function () {
    return jest.fn();
});
describe('Segment.io', function () {
    var options;
    var analytics;
    var segment;
    var spyMock;
    beforeEach(function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    jest.resetAllMocks();
                    jest.restoreAllMocks();
                    options = { apiKey: 'oq0vdlg7yi' };
                    analytics = new Analytics({ writeKey: options.apiKey });
                    segment = segmentio(analytics, options, {});
                    return [4 /*yield*/, analytics.register(segment, pageEnrichment)];
                case 1:
                    _a.sent();
                    window.localStorage.clear();
                    spyMock = mocked(unfetch).mockResolvedValue({
                        ok: true,
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    function resetCookies() {
        Object.keys(cookie.get()).map(function (key) { return cookie.remove(key); });
    }
    afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            analytics.reset();
            resetCookies();
            window.localStorage.clear();
            return [2 /*return*/];
        });
    }); });
    describe('#page', function () {
        it('should enqueue section, name and properties', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, analytics.page('section', 'name', { property: true }, { opt: true })];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/p\"");
                        body = JSON.parse(params.body);
                        assert(body.name === 'name');
                        assert(body.category === 'section');
                        assert(body.properties.property === true);
                        assert(body.context.opt === true);
                        assert(body.timestamp);
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('#identify', function () {
        it('should enqueue an id and traits', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, analytics.identify('id', { trait: true }, { opt: true })];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/i\"");
                        body = JSON.parse(params.body);
                        assert(body.userId === 'id');
                        assert(body.traits.trait === true);
                        assert(body.context.opt === true);
                        assert(body.timestamp);
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('#track', function () {
        it('should enqueue an event and properties', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, analytics.track('event', { prop: true }, { opt: true })];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/t\"");
                        body = JSON.parse(params.body);
                        assert(body.event === 'event');
                        assert(body.context.opt === true);
                        assert(body.properties.prop === true);
                        assert(body.traits == null);
                        assert(body.timestamp);
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('#group', function () {
        it('should enqueue groupId and traits', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, analytics.group('id', { trait: true }, { opt: true })];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/g\"");
                        body = JSON.parse(params.body);
                        assert(body.groupId === 'id');
                        assert(body.context.opt === true);
                        assert(body.traits.trait === true);
                        assert(body.timestamp);
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('#alias', function () {
        it('should enqueue .userId and .previousId', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, analytics.alias('to', 'from')];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/a\"");
                        body = JSON.parse(params.body);
                        assert(body.previousId === 'from');
                        assert(body.userId === 'to');
                        assert(body.timestamp);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should fallback to user.anonymousId if .previousId is omitted', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        analytics.user().anonymousId('anon-id');
                        return [4 /*yield*/, analytics.alias('to')];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/a\"");
                        body = JSON.parse(params.body);
                        assert(body.previousId === 'anon-id');
                        assert(body.userId === 'to');
                        assert(body.timestamp);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should fallback to user.anonymousId if .previousId and user.id are falsey', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, analytics.alias('to')];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/a\"");
                        body = JSON.parse(params.body);
                        assert(body.previousId);
                        assert(body.previousId.length === 36);
                        assert(body.userId === 'to');
                        return [2 /*return*/];
                }
            });
        }); });
        it('should rename `.from` and `.to` to `.previousId` and `.userId`', function () { return __awaiter(void 0, void 0, void 0, function () {
            var _a, url, params, body;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, analytics.alias('user-id', 'previous-id')];
                    case 1:
                        _b.sent();
                        _a = spyMock.mock.calls[0], url = _a[0], params = _a[1];
                        expect(url).toMatchInlineSnapshot("\"https://api.segment.io/v1/a\"");
                        body = JSON.parse(params.body);
                        assert(body.previousId === 'previous-id');
                        assert(body.userId === 'user-id');
                        assert(body.from == null);
                        assert(body.to == null);
                        return [2 /*return*/];
                }
            });
        }); });
    });
});
//# sourceMappingURL=index.test.js.map