| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
jest.mock('@wordpress/api-fetch'); |
| 4 |
jest.mock('@shared/api/DataApi', () => ({ recordPluginActivity: jest.fn() })); |
| 5 |
jest.mock('@shared/api/wp', () => ({ enableAutoUpdate: jest.fn() })); |
| 6 |
|
| 7 |
const { activatePlugin } = require('@auto-launch/functions/plugins'); |
| 8 |
|
| 9 |
describe('activatePlugin', () => { |
| 10 |
beforeEach(() => { |
| 11 |
apiFetch.mockReset(); |
| 12 |
}); |
| 13 |
|
| 14 |
// Regression: when a required plugin 500s during install it never lands on |
| 15 |
// disk, so the activate retry's getPlugin lookup comes back empty. The old |
| 16 |
// code destructured `{ plugin }` off that undefined and threw a TypeError |
| 17 |
// out of the catch as an unhandled rejection, aborting the install loop. |
| 18 |
it('resolves without throwing when the plugin is not installed', async () => { |
| 19 |
apiFetch.mockImplementation((opts) => |
| 20 |
opts.method === 'POST' |
| 21 |
? Promise.reject(new Error('500')) |
| 22 |
: Promise.resolve([]), |
| 23 |
); |
| 24 |
|
| 25 |
await expect(activatePlugin('jetbackup')).resolves.toBeUndefined(); |
| 26 |
expect(console).toHaveWarned(); |
| 27 |
expect(console).toHaveErrored(); |
| 28 |
}); |
| 29 |
|
| 30 |
it('retries activation against the resolved plugin path when found', async () => { |
| 31 |
apiFetch.mockImplementation((opts) => { |
| 32 |
if (opts.method !== 'POST') { |
| 33 |
return Promise.resolve([{ plugin: 'jetbackup/jetbackup.php' }]); |
| 34 |
} |
| 35 |
// The bare-slug attempt fails; the resolved-path retry succeeds. |
| 36 |
return opts.path === '/wp/v2/plugins/jetbackup' |
| 37 |
? Promise.reject(new Error('400')) |
| 38 |
: Promise.resolve(); |
| 39 |
}); |
| 40 |
|
| 41 |
await expect(activatePlugin('jetbackup')).resolves.toBeUndefined(); |
| 42 |
expect(apiFetch).toHaveBeenCalledWith( |
| 43 |
expect.objectContaining({ |
| 44 |
path: '/wp/v2/plugins/jetbackup/jetbackup.php', |
| 45 |
method: 'POST', |
| 46 |
}), |
| 47 |
); |
| 48 |
expect(console).toHaveWarned(); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|