UNPKG

@thoughtspot/visual-embed-sdk

Version:
218 lines 9.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); const types_1 = require("./types"); describe('unit test for utils', () => { test('getQueryParamString', () => { expect((0, utils_1.getQueryParamString)({ foo: 'bar', baz: '42', })).toBe('foo=bar&baz=42'); expect((0, utils_1.getQueryParamString)({})).toBe(null); // should not add undefined params expect((0, utils_1.getQueryParamString)({ foo: undefined, bar: 'baz', })).toBe('bar=baz'); }); test('getFilterQuery should encode URL params', () => { expect((0, utils_1.getFilterQuery)([])).toBe(null); expect((0, utils_1.getFilterQuery)([ { columnName: 'foo+foo', operator: types_1.RuntimeFilterOp.NE, values: ['bar+'], }, ])).toBe('col1=foo%2Bfoo&op1=NE&val1=bar%2B'); }); test('getFilterQuery', () => { expect((0, utils_1.getFilterQuery)([])).toBe(null); expect((0, utils_1.getFilterQuery)([ { columnName: 'foo', operator: types_1.RuntimeFilterOp.NE, values: ['bar'], }, ])).toBe('col1=foo&op1=NE&val1=bar'); const filters = [ { columnName: 'foo', operator: types_1.RuntimeFilterOp.EQ, values: [42], }, { columnName: 'bar', operator: types_1.RuntimeFilterOp.BW_INC, values: [1, 10], }, { columnName: 'baz', operator: types_1.RuntimeFilterOp.CONTAINS, values: ['abc'], }, ]; expect((0, utils_1.getFilterQuery)(filters)).toBe('col1=foo&op1=EQ&val1=42&col2=bar&op2=BW_INC&val2=1&val2=10&col3=baz&op3=CONTAINS&val3=abc'); }); test('getParameterOverride', () => { expect((0, utils_1.getRuntimeParameters)([])).toBe(null); expect((0, utils_1.getRuntimeParameters)([ { name: 'foo', value: 'bar', }, ])).toBe('param1=foo&paramVal1=bar'); const params = [ { name: 'foo', value: 42, }, { name: 'bar', value: 'abc', }, { name: 'baz', value: true, }, ]; expect((0, utils_1.getRuntimeParameters)(params)).toBe('param1=foo&paramVal1=42&param2=bar&paramVal2=abc&param3=baz&paramVal3=true'); }); test('getCssDimension', () => { expect((0, utils_1.getCssDimension)(100)).toBe('100px'); expect((0, utils_1.getCssDimension)('100%')).toBe('100%'); expect((0, utils_1.getCssDimension)('100px')).toBe('100px'); expect((0, utils_1.getCssDimension)(null)).toBe(null); }); test('appendToUrlHash', () => { expect((0, utils_1.appendToUrlHash)('http://myhost:3000', 'hashFrag')).toBe('http://myhost:3000#?tsSSOMarker=hashFrag'); expect((0, utils_1.appendToUrlHash)('http://xyz.com/#foo', 'bar')).toBe('http://xyz.com/#foo?tsSSOMarker=bar'); }); describe('getRedirectURL', () => { let windowSpy; beforeEach(() => { windowSpy = jest.spyOn(window, 'window', 'get'); }); afterEach(() => { windowSpy.mockRestore(); }); test('Should return correct value when path is undefined', () => { expect((0, utils_1.getRedirectUrl)('http://myhost:3000', 'hashFrag')).toBe('http://myhost:3000#?tsSSOMarker=hashFrag'); expect((0, utils_1.getRedirectUrl)('http://xyz.com/#foo', 'bar')).toBe('http://xyz.com/#foo?tsSSOMarker=bar'); }); test('Should return correct value when path is set', () => { windowSpy.mockImplementation(() => ({ location: { origin: 'http://myhost:3000', }, })); expect((0, utils_1.getRedirectUrl)('http://myhost:3000/', 'hashFrag', '/bar')).toBe('http://myhost:3000/bar#?tsSSOMarker=hashFrag'); expect((0, utils_1.getRedirectUrl)('http://myhost:3000/#/foo', 'hashFrag', '#/bar')).toBe('http://myhost:3000/#/bar?tsSSOMarker=hashFrag'); }); }); test('getEncodedQueryParamsString', () => { expect((0, utils_1.getEncodedQueryParamsString)('')).toBe(''); expect((0, utils_1.getEncodedQueryParamsString)('test')).toBe('dGVzdA'); }); test('when ReleaseVersion is empty ', () => { expect((0, utils_1.checkReleaseVersionInBeta)('', false)).toBe(false); }); test('when ReleaseVersion is 7.0.1.cl ', () => { expect((0, utils_1.checkReleaseVersionInBeta)('7.0.1.cl', false)).toBe(false); }); test('when cluster has dev version', () => { expect((0, utils_1.checkReleaseVersionInBeta)('dev', false)).toBe(false); }); test('when cluster is above 8.4.0.cl-11 software version', () => { expect((0, utils_1.checkReleaseVersionInBeta)('8.4.0.cl-117', false)).toBe(false); }); test('when cluster is bellow 8.0.0.sw software version', () => { expect((0, utils_1.checkReleaseVersionInBeta)('7.2.1.sw', false)).toBe(true); }); test('when suppressBetaWarning is true and ReleaseVersion is 7.0.1', () => { expect((0, utils_1.checkReleaseVersionInBeta)('7.0.1', true)).toBe(false); }); test('when suppressBetaWarning is false ReleaseVersion is 7.0.1', () => { expect((0, utils_1.checkReleaseVersionInBeta)('7.0.1', false)).toBe(true); }); test('removeTypename should removed __typename', () => { const input = { test: 'test', __typename: 'should be removed', obj: { test: 'test', __typename: 'should be removed', }, }; const result = (0, utils_1.removeTypename)(input); const expectedResult = { test: 'test', obj: { test: 'test', }, }; expect(result).toEqual(expectedResult); }); describe('validate removeStyleProperties', () => { it('should remove specified style properties from an HTML element', () => { const element = document.createElement('div'); element.style.backgroundColor = 'blue'; element.style.fontSize = '14px'; const propertiesToRemove = ['background-color', 'font-size']; (0, utils_1.removeStyleProperties)(element, propertiesToRemove); expect(element.style.backgroundColor).toBe(''); expect(element.style.fontSize).toBe(''); }); it('should handle undefined param', () => { expect(() => { (0, utils_1.removeStyleProperties)(undefined, []); }).not.toThrow(); }); it('should handle removing non-existent style properties', () => { const element = document.createElement('div'); element.style.backgroundColor = 'blue'; element.style.fontSize = '14px'; const propertiesToRemove = ['color', 'border']; (0, utils_1.removeStyleProperties)(element, propertiesToRemove); expect(element.style.backgroundColor).toBe('blue'); expect(element.style.fontSize).toBe('14px'); }); }); describe('validate setStyleProperties', () => { it('should set style properties on an HTML element', () => { const element = document.createElement('div'); const styles = { backgroundColor: 'red', fontSize: '16px', }; (0, utils_1.setStyleProperties)(element, styles); expect(element.style.backgroundColor).toBe('red'); expect(element.style.fontSize).toBe('16px'); }); it('should handle undefined param', () => { // should not throw an error expect(() => { (0, utils_1.setStyleProperties)(undefined, {}); }).not.toThrow(); }); }); test('isUndefined', () => { expect((0, utils_1.isUndefined)(undefined)).toBe(true); expect((0, utils_1.isUndefined)({})).toBe(false); }); describe('getValueFromWindow and storeValueInWindow', () => { test('Store and retrieve', () => { (0, utils_1.storeValueInWindow)('test', 'testValue'); expect((0, utils_1.getValueFromWindow)('test')).toBe('testValue'); }); test('Object should be set if not', () => { // eslint-disable-next-line no-underscore-dangle window._tsEmbedSDK = null; (0, utils_1.storeValueInWindow)('test', 'testValue'); expect((0, utils_1.getValueFromWindow)('test')).toBe('testValue'); }); test('Return undefined if key is not found', () => { expect((0, utils_1.getValueFromWindow)('notFound')).toBe(undefined); }); }); }); //# sourceMappingURL=utils.spec.js.map