"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const cloneDeep_1 = tslib_1.__importDefault(require("lodash/cloneDeep"));
const filter_1 = require("./filter");
describe('compress and uncompress dashboard filters', () => {
    describe('compressDashboardFiltersToParam', () => {
        const DUMMY_DIMENSION = {
            id: 'filter-id',
            label: 'A label',
            operator: filter_1.FilterOperator.EQUALS,
            target: {
                fieldId: 'payments_payment_method',
                tableName: 'payments',
            },
            tileTargets: {},
            disabled: false,
            values: ['credit_card'],
        };
        it('should create no tile targets when there are no overrides', async () => {
            expect((0, filter_1.compressDashboardFiltersToParam)({
                dimensions: [DUMMY_DIMENSION],
                metrics: [],
                tableCalculations: [],
            })).toEqual({
                dimensions: [
                    {
                        id: 'filter-id',
                        label: 'A label',
                        operator: 'equals',
                        target: {
                            fieldId: 'payments_payment_method',
                            tableName: 'payments',
                        },
                        tileTargets: [],
                        disabled: false,
                        values: ['credit_card'],
                    },
                ],
                metrics: [],
                tableCalculations: [],
            });
        });
        it('should set tile target to "false" when tile is disabled', async () => {
            expect((0, filter_1.compressDashboardFiltersToParam)({
                dimensions: [
                    {
                        ...DUMMY_DIMENSION,
                        tileTargets: { 'chart-id-no-filter': false },
                    },
                ],
                metrics: [],
                tableCalculations: [],
            }).dimensions[0].tileTargets).toEqual([{ 'chart-id-no-filter': false }]);
        });
        it('should set tile target overrides when filter fields dont match tile fields', async () => {
            expect((0, filter_1.compressDashboardFiltersToParam)({
                dimensions: [
                    {
                        ...DUMMY_DIMENSION,
                        tileTargets: {
                            'chart-id-modified-filter': {
                                fieldId: 'a_different_field',
                                tableName: 'a_different_table',
                            },
                        },
                    },
                ],
                metrics: [],
                tableCalculations: [],
            }).dimensions[0].tileTargets).toEqual([
                {
                    'chart-id-modified-filter': {
                        fieldId: 'a_different_field',
                        tableName: 'a_different_table',
                    },
                },
            ]);
        });
        it('should NOT set tile targets when filter fields DO match tile fields', async () => {
            expect((0, filter_1.compressDashboardFiltersToParam)({
                dimensions: [
                    {
                        ...DUMMY_DIMENSION,
                        tileTargets: {
                            'chart-id': {
                                fieldId: 'payments_payment_method',
                                tableName: 'payments',
                            },
                        },
                    },
                ],
                metrics: [],
                tableCalculations: [],
            }).dimensions[0].tileTargets).toEqual([]);
        });
        it('should handle disabled, override and default cases together', async () => {
            const compressedFilters = (0, filter_1.compressDashboardFiltersToParam)({
                dimensions: [
                    {
                        ...DUMMY_DIMENSION,
                        tileTargets: {
                            'chart-id': {
                                fieldId: 'payments_payment_method',
                                tableName: 'payments',
                            },
                            'chart-id-no-filter': false,
                            'chart-id-modified-filter': {
                                fieldId: 'a_different_field',
                                tableName: 'a_different_table',
                            },
                            'chart-id-no-filter2': false,
                        },
                    },
                ],
                metrics: [
                    {
                        ...DUMMY_DIMENSION,
                        tileTargets: {
                            'metric-chart-id': {
                                fieldId: 'payments_payment_method',
                                tableName: 'payments',
                            },
                            'metric-chart-id-no-filter': false,
                            'metric-chart-id-modified-filter': {
                                fieldId: 'a_different_field',
                                tableName: 'a_different_table',
                            },
                            'metric-chart-id-no-filter2': false,
                        },
                    },
                ],
                tableCalculations: [],
            });
            expect(compressedFilters.dimensions[0].tileTargets).toEqual([
                { 'chart-id-no-filter': false },
                {
                    'chart-id-modified-filter': {
                        fieldId: 'a_different_field',
                        tableName: 'a_different_table',
                    },
                },
                { 'chart-id-no-filter2': false },
            ]);
            expect(compressedFilters.metrics[0].tileTargets).toEqual([
                { 'metric-chart-id-no-filter': false },
                {
                    'metric-chart-id-modified-filter': {
                        fieldId: 'a_different_field',
                        tableName: 'a_different_table',
                    },
                },
                { 'metric-chart-id-no-filter2': false },
            ]);
        });
    });
    describe('convertDashboardFiltersParamToDashboardFilters', () => {
        const DUMMY_URL_FILTER = {
            id: 'url-dimension',
            label: 'a label',
            operator: filter_1.FilterOperator.EQUALS,
            target: {
                fieldId: 'payments_payment_method',
                tableName: 'payments',
            },
            tileTargets: [],
            disabled: false,
            values: ['credit_card'],
        };
        it('should convert filters with no tile targets', async () => {
            expect((0, filter_1.convertDashboardFiltersParamToDashboardFilters)({
                dimensions: [DUMMY_URL_FILTER],
                metrics: [],
                tableCalculations: [],
            })).toEqual({
                dimensions: [
                    {
                        id: 'url-dimension',
                        label: 'a label',
                        operator: 'equals',
                        target: {
                            fieldId: 'payments_payment_method',
                            tableName: 'payments',
                        },
                        tileTargets: {},
                        disabled: false,
                        values: ['credit_card'],
                    },
                ],
                metrics: [],
                tableCalculations: [],
            });
        });
        it('should have modified tile targets when target modified', async () => {
            expect((0, filter_1.convertDashboardFiltersParamToDashboardFilters)({
                dimensions: [
                    {
                        ...DUMMY_URL_FILTER,
                        tileTargets: [
                            {
                                'chart-id-modified-filter': {
                                    fieldId: 'other-field',
                                    tableName: 'other-table',
                                },
                            },
                        ],
                    },
                ],
                metrics: [],
                tableCalculations: [],
            }).dimensions[0].tileTargets).toEqual({
                'chart-id-modified-filter': {
                    fieldId: 'other-field',
                    tableName: 'other-table',
                },
            });
        });
        it('should have disabled tile targets when target is "false"', async () => {
            expect((0, filter_1.convertDashboardFiltersParamToDashboardFilters)({
                dimensions: [
                    {
                        ...DUMMY_URL_FILTER,
                        tileTargets: [{ 'chart-id-no-filter': false }],
                    },
                ],
                metrics: [],
                tableCalculations: [],
            }).dimensions[0].tileTargets).toEqual({ 'chart-id-no-filter': false });
        });
        it('should omit tile target string (for back-compat)', async () => {
            expect((0, filter_1.convertDashboardFiltersParamToDashboardFilters)({
                dimensions: [
                    {
                        ...DUMMY_URL_FILTER,
                        tileTargets: ['an-id'],
                    },
                ],
                metrics: [],
                tableCalculations: [],
            }).dimensions[0].tileTargets).toEqual({});
        });
        it('should round-trip requirement fields through compress and convert', async () => {
            const dimensions = [
                {
                    id: 'group-member',
                    label: undefined,
                    operator: filter_1.FilterOperator.EQUALS,
                    target: {
                        fieldId: 'orders_status',
                        tableName: 'orders',
                    },
                    tileTargets: {},
                    disabled: true,
                    required: true,
                    requiredGroupId: 'group-1',
                    values: [],
                },
            ];
            const roundTripped = (0, filter_1.convertDashboardFiltersParamToDashboardFilters)((0, filter_1.compressDashboardFiltersToParam)({
                dimensions,
                metrics: [],
                tableCalculations: [],
            }));
            expect(roundTripped.dimensions).toEqual(dimensions);
        });
        it('should handle normal, modified and disabled tile targets', async () => {
            expect((0, filter_1.convertDashboardFiltersParamToDashboardFilters)({
                dimensions: [
                    {
                        ...DUMMY_URL_FILTER,
                        tileTargets: [
                            'an-id',
                            { 'chart-id-no-filter': false },
                            'another-id',
                            {
                                'chart-id-modified-filter': {
                                    fieldId: 'other-field',
                                    tableName: 'other-table',
                                },
                            },
                        ],
                    },
                ],
                metrics: [],
                tableCalculations: [],
            }).dimensions[0].tileTargets).toEqual({
                'chart-id-no-filter': false,
                'chart-id-modified-filter': {
                    fieldId: 'other-field',
                    tableName: 'other-table',
                },
            });
        });
    });
});
describe('removeFieldFromFilterGroup', () => {
    const fieldToBeRemoved = 'metric_field_id_1';
    const filterGroup = {
        id: 'metric_id_1',
        and: [
            {
                id: 'metric_uuid_1',
                target: {
                    fieldId: fieldToBeRemoved,
                },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['metric_value_1'],
            },
            {
                id: 'metric_id_2',
                and: [
                    {
                        id: 'metric_uuid_2',
                        target: {
                            fieldId: fieldToBeRemoved,
                        },
                        operator: filter_1.FilterOperator.EQUALS,
                        values: ['metric_value_2'],
                    },
                ],
            },
        ],
    };
    it('should return empty filters', async () => {
        const updatedFieldGroup = (0, filter_1.removeFieldFromFilterGroup)(filterGroup, fieldToBeRemoved);
        expect(updatedFieldGroup).toEqual(undefined);
    });
    it('should return remaining filters', async () => {
        const remainingMetric = {
            id: 'metric_uuid_3',
            target: {
                fieldId: 'metric_field_id_3',
            },
            operator: filter_1.FilterOperator.EQUALS,
            values: ['metric_value_3'],
        };
        const filterGroupWithRemainingMetrics = (0, cloneDeep_1.default)(filterGroup);
        filterGroupWithRemainingMetrics.and[1].and.push(remainingMetric);
        filterGroupWithRemainingMetrics.and.push(remainingMetric);
        const updatedFieldGroup = (0, filter_1.removeFieldFromFilterGroup)(filterGroupWithRemainingMetrics, fieldToBeRemoved);
        expect(updatedFieldGroup).toEqual({
            id: 'metric_id_1',
            and: [
                {
                    id: 'metric_id_2',
                    and: [remainingMetric],
                },
                remainingMetric,
            ],
        });
    });
});
describe('isFilterRuleDefinedForFieldId', () => {
    const fieldToBeFound1 = 'metric_field_id_1';
    const fieldToBeFound2 = 'metric_field_id_2';
    const fieldToBeFound3 = 'metric_field_id_3';
    const filterGroup = {
        id: 'metric_id_1',
        and: [
            {
                id: 'metric_uuid_1',
                target: {
                    fieldId: fieldToBeFound1,
                },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['metric_value_1'],
            },
            {
                id: 'metric_uuid_3',
                target: {
                    fieldId: `${fieldToBeFound3}_to_be_found`,
                },
                operator: filter_1.FilterOperator.EQUALS,
                values: ['metric_value_3'],
            },
            {
                id: 'metric_id_2',
                and: [
                    {
                        id: 'metric_uuid_2',
                        target: {
                            fieldId: fieldToBeFound2,
                        },
                        operator: filter_1.FilterOperator.EQUALS,
                        values: ['metric_value_2'],
                    },
                ],
            },
        ],
    };
    it('should find', async () => {
        expect((0, filter_1.isFilterRuleDefinedForFieldId)(filterGroup, fieldToBeFound1)).toEqual(true);
        expect((0, filter_1.isFilterRuleDefinedForFieldId)(filterGroup, fieldToBeFound2)).toEqual(true);
        expect((0, filter_1.isFilterRuleDefinedForFieldId)(filterGroup, 'someRandomFieldId')).toEqual(false);
        expect((0, filter_1.isFilterRuleDefinedForFieldId)(filterGroup, fieldToBeFound3, true)).toEqual(true);
        expect((0, filter_1.isFilterRuleDefinedForFieldId)(filterGroup, fieldToBeFound3, false)).toEqual(false);
    });
});
//# sourceMappingURL=filter.test.js.map