Fixes broken flow

This commit is contained in:
Nithin David Thomas
2024-02-15 00:23:16 +05:30
parent 54bd3c38d5
commit f598b6886f
8 changed files with 174 additions and 50 deletions
@@ -0,0 +1,13 @@
export const getBrowserLocale = enabledLanguages => {
const localeWithVariant = window.navigator.language.replace('-', '_');
const localeWithoutVariant = localeWithVariant.split('_')[0];
return enabledLanguages.find(
lang =>
lang.iso_639_1_code === localeWithVariant ||
lang.iso_639_1_code === localeWithoutVariant
)?.iso_639_1_code;
};
export const getBrowserTimezone = () => {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
};
@@ -0,0 +1,15 @@
export const findMatchingOption = (value, options, defaultValue) => {
const match = options.find(
option => option.value === value || option === value
);
return match ? match.value || match : defaultValue;
};
export const findCompanySizeMatch = (options, size) => {
return (
options.find(option => {
const upperLimit = option.value.split('-')[1]?.split('+')[0];
return size < (upperLimit ? Number(upperLimit) : Infinity);
})?.value || this.companySizeOptions[0].value
);
};
@@ -0,0 +1,43 @@
import { getBrowserLocale, getBrowserTimezone } from '../BrowserHelper';
describe('getBrowserLocale', () => {
it('should return the correct locale code when an exact match is found', () => {
// Mock window.navigator.language
Object.defineProperty(window.navigator, 'language', {
value: 'en-US',
configurable: true,
});
const languages = [{ iso_639_1_code: 'en' }, { iso_639_1_code: 'en-US' }];
expect(getBrowserLocale(languages)).toBe('en-US');
});
it('should return the correct locale code when only a partial match is found', () => {
Object.defineProperty(window.navigator, 'language', {
value: 'en-GB',
configurable: true,
});
const languages = [{ iso_639_1_code: 'en' }, { iso_639_1_code: 'fr' }];
expect(getBrowserLocale(languages)).toBe('en');
});
it('should return undefined when no match is found', () => {
Object.defineProperty(window.navigator, 'language', {
value: 'es-ES',
configurable: true,
});
const languages = [{ iso_639_1_code: 'en' }, { iso_639_1_code: 'fr' }];
expect(getBrowserLocale(languages)).toBeUndefined();
});
});
describe('getBrowserTimezone', () => {
it('should return the current browser timezone', () => {
// This test assumes the environment's timezone is set.
// It's a basic test as getBrowserTimezone relies on Intl API.
const timezone = getBrowserTimezone();
expect(timezone).toBe(Intl.DateTimeFormat().resolvedOptions().timeZone);
});
});
@@ -0,0 +1,35 @@
import { findMatchingOption, findCompanySizeMatch } from '../OnboardingHelper';
describe('findMatchingOption', () => {
const options = [{ value: 'option1' }, { value: 'option2' }];
it('should return the matching option', () => {
expect(findMatchingOption('option1', options, 'default')).toBe('option1');
});
it('should return the default value when no match is found', () => {
expect(findMatchingOption('nonExistingOption', options, 'default')).toBe(
'default'
);
});
});
describe('findCompanySizeMatch', () => {
const mockCompanySizeOptions = [
{ value: '1-10' },
{ value: '11-50' },
{ value: '51-500' },
{ value: '501-1000' },
{ value: '1001+' },
];
it('should return the correct company size range', () => {
const size = 25;
expect(findCompanySizeMatch(size, mockCompanySizeOptions)).toBe('11-50');
});
it('should return the first company size range when no match is found', () => {
const size = 1500;
expect(findCompanySizeMatch(size, mockCompanySizeOptions)).toBe('1-10');
});
});