|
| 1 | +import { clientLocale } from '../config/env.json'; |
| 2 | +import { |
| 3 | + convertToLocalizedString, |
| 4 | + generateSearchPlaceholder, |
| 5 | + roundDownToNearestHundred |
| 6 | +} from './generate-search-placeholder'; |
| 7 | + |
| 8 | +describe('Search bar placeholder tests:', () => { |
| 9 | + describe('Number rounding', () => { |
| 10 | + test('Numbers less than 100 return 0', () => { |
| 11 | + const testArr = [0, 1, 50, 99]; |
| 12 | + |
| 13 | + testArr.forEach(num => { |
| 14 | + expect(roundDownToNearestHundred(num)).toEqual(0); |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + test('Numbers greater than 100 return a number rounded down to the nearest 100', () => { |
| 19 | + const testArr = [ |
| 20 | + { |
| 21 | + num: 100, |
| 22 | + expected: 100 |
| 23 | + }, |
| 24 | + { |
| 25 | + num: 101, |
| 26 | + expected: 100 |
| 27 | + }, |
| 28 | + { |
| 29 | + num: 199, |
| 30 | + expected: 100 |
| 31 | + }, |
| 32 | + { |
| 33 | + num: 999, |
| 34 | + expected: 900 |
| 35 | + }, |
| 36 | + { |
| 37 | + num: 1000, |
| 38 | + expected: 1000 |
| 39 | + }, |
| 40 | + { |
| 41 | + num: 1001, |
| 42 | + expected: 1000 |
| 43 | + }, |
| 44 | + { |
| 45 | + num: 1999, |
| 46 | + expected: 1900 |
| 47 | + }, |
| 48 | + { |
| 49 | + num: 10000, |
| 50 | + expected: 10000 |
| 51 | + }, |
| 52 | + { |
| 53 | + num: 10001, |
| 54 | + expected: 10000 |
| 55 | + }, |
| 56 | + { |
| 57 | + num: 19999, |
| 58 | + expected: 19900 |
| 59 | + } |
| 60 | + ]; |
| 61 | + |
| 62 | + testArr.forEach(obj => { |
| 63 | + expect(roundDownToNearestHundred(obj.num)).toEqual(obj.expected); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Number formatting', () => { |
| 69 | + test('Numbers are converted to the correct decimal or comma format for each locale', () => { |
| 70 | + const testArr = [ |
| 71 | + { |
| 72 | + num: 100, |
| 73 | + locale: 'en', |
| 74 | + expected: '100' |
| 75 | + }, |
| 76 | + { |
| 77 | + num: 100, |
| 78 | + locale: 'zh', |
| 79 | + expected: '100' |
| 80 | + }, |
| 81 | + { |
| 82 | + num: 100, |
| 83 | + locale: 'de', |
| 84 | + expected: '100' |
| 85 | + }, |
| 86 | + { |
| 87 | + num: 1000, |
| 88 | + locale: 'en', |
| 89 | + expected: '1,000' |
| 90 | + }, |
| 91 | + { |
| 92 | + num: 1000, |
| 93 | + locale: 'zh', |
| 94 | + expected: '1,000' |
| 95 | + }, |
| 96 | + { |
| 97 | + num: 1000, |
| 98 | + locale: 'de', |
| 99 | + expected: '1.000' |
| 100 | + }, |
| 101 | + { |
| 102 | + num: 10000, |
| 103 | + locale: 'en', |
| 104 | + expected: '10,000' |
| 105 | + }, |
| 106 | + { |
| 107 | + num: 10000, |
| 108 | + locale: 'zh', |
| 109 | + expected: '10,000' |
| 110 | + }, |
| 111 | + { |
| 112 | + num: 10000, |
| 113 | + locale: 'de', |
| 114 | + expected: '10.000' |
| 115 | + }, |
| 116 | + { |
| 117 | + num: 100000, |
| 118 | + locale: 'en', |
| 119 | + expected: '100,000' |
| 120 | + }, |
| 121 | + { |
| 122 | + num: 100000, |
| 123 | + locale: 'zh', |
| 124 | + expected: '100,000' |
| 125 | + }, |
| 126 | + { |
| 127 | + num: 100000, |
| 128 | + locale: 'de', |
| 129 | + expected: '100.000' |
| 130 | + } |
| 131 | + ]; |
| 132 | + |
| 133 | + testArr.forEach(obj => { |
| 134 | + const { num, locale, expected } = obj; |
| 135 | + expect(convertToLocalizedString(num, locale)).toEqual(expected); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + // Note: Only test the English locale to prevent duplicate tests, |
| 141 | + // and just to ensure the logic is working as expected. |
| 142 | + if (clientLocale === 'english') { |
| 143 | + describe('Placeholder strings', () => { |
| 144 | + test('When the total number of hits is less than 100 the expected placeholder is generated', async () => { |
| 145 | + const expected = 'Search our tutorials'; |
| 146 | + const placeholderText = await generateSearchPlaceholder({ |
| 147 | + mockRecordsNum: 99, |
| 148 | + locale: 'english' |
| 149 | + }); |
| 150 | + |
| 151 | + expect(placeholderText).toEqual(expected); |
| 152 | + }); |
| 153 | + |
| 154 | + test('When the total number of hits is equal to 100 the expected placeholder is generated', async () => { |
| 155 | + const placeholderText = await generateSearchPlaceholder({ |
| 156 | + mockRecordsNum: 100, |
| 157 | + locale: 'english' |
| 158 | + }); |
| 159 | + const expected = 'Search 100+ tutorials'; |
| 160 | + |
| 161 | + expect(placeholderText).toEqual(expected); |
| 162 | + }); |
| 163 | + |
| 164 | + test('When the total number of hits is greater than 100 the expected placeholder is generated', async () => { |
| 165 | + const placeholderText = await generateSearchPlaceholder({ |
| 166 | + mockRecordsNum: 11000, |
| 167 | + locale: 'english' |
| 168 | + }); |
| 169 | + const expected = 'Search 11,000+ tutorials'; |
| 170 | + |
| 171 | + expect(placeholderText).toEqual(expected); |
| 172 | + }); |
| 173 | + }); |
| 174 | + } |
| 175 | +}); |
0 commit comments