| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import * as wpAPI from '@shared/api/wp'; |
| 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 calls correct POST endpoint', async () => { |
| 22 |
apiFetch.mockResolvedValueOnce({ success: true }); |
| 23 |
|
| 24 |
const result = await wpAPI.installPlugin('my-plugin'); |
| 25 |
expect(apiFetch).toHaveBeenCalledWith({ |
| 26 |
path: '/wp/v2/plugins', |
| 27 |
method: 'POST', |
| 28 |
data: { slug: 'my-plugin' }, |
| 29 |
}); |
| 30 |
expect(result).toEqual({ success: true }); |
| 31 |
}); |
| 32 |
|
| 33 |
it('activatePlugin calls correct POST endpoint', async () => { |
| 34 |
apiFetch.mockResolvedValueOnce([{ name: 'plugin-x' }]); |
| 35 |
apiFetch.mockResolvedValueOnce({ activated: true }); |
| 36 |
|
| 37 |
const result = await wpAPI.activatePlugin('plugin-x'); |
| 38 |
expect(apiFetch).toHaveBeenCalledTimes(2); |
| 39 |
expect(result).toEqual({ activated: true }); |
| 40 |
}); |
| 41 |
}); |
| 42 |
|