|
| 1 | +// Tests in-flight de-duplication of the groups/fetch action. |
| 2 | +// |
| 3 | +// The action is called eagerly by GroupsPage (one dispatch per yourGroups id) |
| 4 | +// and again whenever a user navigates to a group page. Without de-dup, the |
| 5 | +// same id can be in flight twice concurrently. |
| 6 | + |
| 7 | +jest.mock('axios', () => ({ |
| 8 | + __esModule: true, |
| 9 | + default: { |
| 10 | + get: jest.fn(), |
| 11 | + post: jest.fn(), |
| 12 | + patch: jest.fn(), |
| 13 | + delete: jest.fn(), |
| 14 | + }, |
| 15 | +})) |
| 16 | +import axios from 'axios' |
| 17 | + |
| 18 | +import groups from './groups' |
| 19 | + |
| 20 | +// store/groups.js reads locale via document.getElementById('language-current').innerText. |
| 21 | +// jsdom doesn't populate innerText reliably, so set the property explicitly. |
| 22 | +beforeEach(() => { |
| 23 | + document.body.innerHTML = '<div id="language-current"></div>' |
| 24 | + const el = document.getElementById('language-current') |
| 25 | + Object.defineProperty(el, 'innerText', { value: 'en', configurable: true }) |
| 26 | + axios.get.mockReset() |
| 27 | +}) |
| 28 | + |
| 29 | +function commit() {} |
| 30 | +const rootGetters = { 'auth/apiToken': 'TEST' } |
| 31 | + |
| 32 | +function deferred() { |
| 33 | + let resolve, reject |
| 34 | + const promise = new Promise((res, rej) => { resolve = res; reject = rej }) |
| 35 | + return { promise, resolve, reject } |
| 36 | +} |
| 37 | + |
| 38 | +test('two concurrent fetches for the same group share a single in-flight request', async () => { |
| 39 | + const d = deferred() |
| 40 | + axios.get.mockReturnValueOnce(d.promise) |
| 41 | + |
| 42 | + const a = groups.actions.fetch({ rootGetters, commit }, { id: 42 }) |
| 43 | + const b = groups.actions.fetch({ rootGetters, commit }, { id: 42 }) |
| 44 | + |
| 45 | + expect(axios.get).toHaveBeenCalledTimes(1) |
| 46 | + |
| 47 | + d.resolve({ data: { data: { id: 42, name: 'G' } } }) |
| 48 | + await Promise.all([a, b]) |
| 49 | + |
| 50 | + expect(axios.get).toHaveBeenCalledTimes(1) |
| 51 | +}) |
| 52 | + |
| 53 | +test('a new fetch after the previous one settled hits the network again', async () => { |
| 54 | + axios.get.mockResolvedValueOnce({ data: { data: { id: 7, name: 'G7' } } }) |
| 55 | + await groups.actions.fetch({ rootGetters, commit }, { id: 7 }) |
| 56 | + expect(axios.get).toHaveBeenCalledTimes(1) |
| 57 | + |
| 58 | + axios.get.mockResolvedValueOnce({ data: { data: { id: 7, name: 'G7 again' } } }) |
| 59 | + await groups.actions.fetch({ rootGetters, commit }, { id: 7 }) |
| 60 | + expect(axios.get).toHaveBeenCalledTimes(2) |
| 61 | +}) |
| 62 | + |
| 63 | +test('concurrent fetches for different groups each get their own request', async () => { |
| 64 | + const d1 = deferred() |
| 65 | + const d2 = deferred() |
| 66 | + axios.get.mockReturnValueOnce(d1.promise).mockReturnValueOnce(d2.promise) |
| 67 | + |
| 68 | + const a = groups.actions.fetch({ rootGetters, commit }, { id: 1 }) |
| 69 | + const b = groups.actions.fetch({ rootGetters, commit }, { id: 2 }) |
| 70 | + |
| 71 | + expect(axios.get).toHaveBeenCalledTimes(2) |
| 72 | + |
| 73 | + d1.resolve({ data: { data: { id: 1 } } }) |
| 74 | + d2.resolve({ data: { data: { id: 2 } } }) |
| 75 | + await Promise.all([a, b]) |
| 76 | +}) |
| 77 | + |
| 78 | +test('a fetch that throws clears its in-flight slot so retries can run', async () => { |
| 79 | + axios.get.mockRejectedValueOnce(new Error('boom')) |
| 80 | + await groups.actions.fetch({ rootGetters, commit }, { id: 9 }) |
| 81 | + |
| 82 | + // The previous fetch is no longer in flight, so a new one re-hits the network. |
| 83 | + axios.get.mockResolvedValueOnce({ data: { data: { id: 9 } } }) |
| 84 | + await groups.actions.fetch({ rootGetters, commit }, { id: 9 }) |
| 85 | + |
| 86 | + expect(axios.get).toHaveBeenCalledTimes(2) |
| 87 | +}) |
| 88 | + |
| 89 | +test('fetches with the same id but different includeStats are independent requests', async () => { |
| 90 | + axios.get.mockResolvedValue({ data: { data: { id: 3 } } }) |
| 91 | + |
| 92 | + await groups.actions.fetch({ rootGetters, commit }, { id: 3, includeStats: false }) |
| 93 | + await groups.actions.fetch({ rootGetters, commit }, { id: 3, includeStats: true }) |
| 94 | + |
| 95 | + expect(axios.get).toHaveBeenCalledTimes(2) |
| 96 | + expect(axios.get.mock.calls[0][0]).toContain('includeStats=false') |
| 97 | + expect(axios.get.mock.calls[1][0]).toContain('includeStats=true') |
| 98 | +}) |
0 commit comments