import {
    buildQueryStringParameterFromArrayOfComplexObjects,
    getLineItemQueryString,
    getTaxLineItemQueryString
} from '@/helpers/querystring-helper';

describe('querystring-helper', () => {
    describe('buildQueryStringParameterFromArrayOfComplexObjects', () => {
        it('returns a query string from a complex object', () => {
            // Arrange
            const object = [{ a: 'a', val: 24 }, { a: 'b', val: '52' }];

            // Act
            const result = buildQueryStringParameterFromArrayOfComplexObjects(object, 'param');

            // Assert
            expect(result).toBe('param[0].a=a&param[0].val=24&param[1].a=b&param[1].val=52');
        });
    });

    describe('getLineItemQueryString', () => {
        it('returns empty query string from empty line items', () => {
            // Arrange
            const lineItems = [];

            // Act
            const result = getLineItemQueryString(lineItems, 'param');

            // Assert
            expect(result).toBe('');
        });

        it('returns a query string from line items', () => {
            // Arrange
            const a = { partNumber: 'a' };
            const c = { partNumber: 'c' };
            const d = { partNumber: 'd' };
            const b = { partNumber: 'b', childParts: [c, d] };
            const e = { partNumber: 'e' };

            const lineItems = [a, b, e];

            // Act
            const result = getLineItemQueryString(lineItems, 'param');

            // Assert
            // eslint-disable-next-line max-len
            expect(result).toBe('&param[0].partNumber=a&param[1].partNumber=b&param[2].partNumber=c&param[3].partNumber=d&param[4].partNumber=e');
        });
    });

    describe('getTaxLineItemQueryString', () => {
        it('returns empty query string from empty line items', () => {
            // Arrange
            const lineItems = [];

            // Act
            const result = getTaxLineItemQueryString(lineItems, 'param');

            // Assert
            expect(result).toBe('');
        });

        it('returns a query string from line items', () => {
            // Arrange
            const a = { partNumber: 'a', laborAmount: 0, sellingPrice: 10, kitPrice: 10 };
            const c = { partNumber: 'c', laborAmount: 5, sellingPrice: 5, kitPrice: 5 };
            const b = { partNumber: 'b', laborAmount: 10, sellingPrice: 10, kitPrice: 0, childParts: [c] };
            const d = { partNumber: 'd', laborAmount: 10, sellingPrice: 10, kitPrice: 10 };

            const lineItems = [a, b, d];

            // Act
            const result = getTaxLineItemQueryString(lineItems, 'param');

            // Assert
            // eslint-disable-next-line max-len
            expect(result).toBe('&param[0].partNumber=a&param[0].laborAmount=0&param[0].sellingPrice=10&param[0].kitPrice=10&param[1].partNumber=b&param[1].laborAmount=10&param[1].sellingPrice=10&param[1].kitPrice=0&param[2].partNumber=c&param[2].laborAmount=5&param[2].sellingPrice=5&param[2].kitPrice=5&param[3].partNumber=d&param[3].laborAmount=10&param[3].sellingPrice=10&param[3].kitPrice=10');
        });
    });
});
