| 1 |
import * as wpAPI from '@shared/api/wp'; |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
|
| 4 |
jest.mock('@wordpress/api-fetch'); |
| 5 |
|
| 6 |
describe('WordPress Plugin API helpers', () => { |
| 7 |
afterEach(() => { |
| 8 |
jest.clearAllMocks(); |
| 9 |
}); |
| 10 |
|
| 11 |
it('getPlugin calls correct endpoint', async () => { |
| 12 |
apiFetch.mockResolvedValueOnce([{ name: 'test-plugin' }]); |
| 13 |
|
| 14 |
const result = await wpAPI.getPlugin('test-plugin'); |
| 15 |
expect(apiFetch).toHaveBeenCalledWith({ |
| 16 |
path: '/wp/v2/plugins?search=test-plugin', |
| 17 |
}); |
| 18 |
expect(result).toEqual({ name: 'test-plugin' }); |
| 19 |
}); |
| 20 |
|
| 21 |
it('installPlugin installs then enables auto-update', async () => { |
| 22 |
apiFetch.mockResolvedValueOnce({ plugin: 'my-plugin/my-plugin' }); |
| 23 |
apiFetch.mockResolvedValueOnce({ success: true }); |
| 24 |
|
| 25 |
const result = await wpAPI.installPlugin('my-plugin'); |
| 26 |
expect(apiFetch).toHaveBeenCalledWith({ |
| 27 |
path: '/wp/v2/plugins', |
| 28 |
method: 'POST', |
| 29 |
data: { slug: 'my-plugin' }, |
| 30 |
}); |
| 31 |
expect(apiFetch).toHaveBeenCalledWith({ |
| 32 |
path: '/extendify/v1/shared/enable-auto-update', |
| 33 |
method: 'POST', |
| 34 |
data: { plugin: 'my-plugin/my-plugin' }, |
| 35 |
}); |
| 36 |
expect(result).toEqual({ plugin: 'my-plugin/my-plugin' }); |
| 37 |
}); |
| 38 |
|
| 39 |
it('activatePlugin calls correct POST endpoint', async () => { |
| 40 |
apiFetch.mockResolvedValueOnce([{ name: 'plugin-x' }]); |
| 41 |
apiFetch.mockResolvedValueOnce({ activated: true }); |
| 42 |
|
| 43 |
const result = await wpAPI.activatePlugin('plugin-x'); |
| 44 |
expect(apiFetch).toHaveBeenCalledTimes(2); |
| 45 |
expect(result).toEqual({ activated: true }); |
| 46 |
}); |
| 47 |
}); |
| 48 |
|