|
| 1 | +import { Users } from './fixtures/userStates'; |
| 2 | +import { expect, test } from './utils/test'; |
| 3 | + |
| 4 | +const DEFAULT_LANGUAGE = 'en'; |
| 5 | +const DIVERGING_LANGUAGE = 'pt-BR'; |
| 6 | +const USERS_SET_PREFERENCES_ENDPOINT = '/api/v1/users.setPreferences'; |
| 7 | + |
| 8 | +test.use({ storageState: Users.user1.state }); |
| 9 | + |
| 10 | +test.describe('user-language-preferences', () => { |
| 11 | + test.beforeAll(async ({ api }) => { |
| 12 | + const response = await api.post('/users.setPreferences', { |
| 13 | + userId: Users.user1.data._id, |
| 14 | + data: { language: DEFAULT_LANGUAGE }, |
| 15 | + }); |
| 16 | + |
| 17 | + expect(response.status()).toBe(200); |
| 18 | + }); |
| 19 | + |
| 20 | + test.afterAll(async ({ api }) => { |
| 21 | + const response = await api.post('/users.setPreferences', { |
| 22 | + userId: Users.user1.data._id, |
| 23 | + data: { language: DEFAULT_LANGUAGE }, |
| 24 | + }); |
| 25 | + |
| 26 | + expect(response.status()).toBe(200); |
| 27 | + }); |
| 28 | + |
| 29 | + test('avoids redundant language preference updates when cache diverges', async ({ page }) => { |
| 30 | + await page.addInitScript(([language, serverLanguage]) => { |
| 31 | + // Emulate a stale tab whose cache disagrees with the server preference. |
| 32 | + window.localStorage.setItem('fuselage-localStorage-userLanguage', JSON.stringify(language)); |
| 33 | + window.localStorage.setItem('fuselage-localStorage-preferedLanguage', JSON.stringify(serverLanguage)); |
| 34 | + }, [DIVERGING_LANGUAGE, DEFAULT_LANGUAGE]); |
| 35 | + |
| 36 | + const languageRequests: string[] = []; |
| 37 | + |
| 38 | + page.on('request', (request) => { |
| 39 | + if (request.method() !== 'POST' || !request.url().includes(USERS_SET_PREFERENCES_ENDPOINT)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + const payload = JSON.parse(request.postData() ?? '{}'); |
| 45 | + const language = payload?.data?.language; |
| 46 | + |
| 47 | + if (language) { |
| 48 | + languageRequests.push(language); |
| 49 | + } |
| 50 | + } catch { |
| 51 | + // Ignore malformed payloads produced outside this scenario. |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + await page.goto('/home'); |
| 56 | + |
| 57 | + await page.waitForTimeout(3000); |
| 58 | + |
| 59 | + expect(languageRequests).toHaveLength(0); |
| 60 | + }); |
| 61 | +}); |
0 commit comments