Update tests of plugins

This commit is contained in:
printempw 2018-06-29 18:26:48 +08:00
parent 4da044146f
commit b4e8b7e8c9
2 changed files with 57 additions and 6 deletions

View File

@ -407,10 +407,20 @@ describe('tests for "plugins" module', () => {
it('enable a plugin', async () => {
const fetch = jest.fn()
.mockReturnValueOnce(Promise.resolve({ requirements: [] }))
.mockReturnValueOnce(Promise.resolve({ requirements: [] }))
.mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' }))
.mockReturnValueOnce(Promise.resolve({
isRequirementsSatisfied: false,
requirements: { 'a': '^1.1.0', 'b': '^2.1.0', 'c': '^3.3.0' },
unsatisfiedRequirements: { 'c': '^3.3.0' }
}))
.mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' }))
.mockReturnValueOnce(Promise.reject());
const url = jest.fn(path => path);
const swal = jest.fn()
.mockReturnValueOnce(Promise.reject())
.mockReturnValueOnce(Promise.resolve());
const toastr = {
success: jest.fn(),
warning: jest.fn()
@ -419,6 +429,7 @@ describe('tests for "plugins" module', () => {
const showAjaxError = jest.fn();
window.fetch = fetch;
window.url = url;
window.swal = swal;
window.toastr = toastr;
window.showAjaxError = showAjaxError;
$.pluginsTable = {
@ -429,18 +440,31 @@ describe('tests for "plugins" module', () => {
const enablePlugin = require(modulePath).enablePlugin;
await enablePlugin('plugin');
expect(fetch).toBeCalledWith({
type: 'POST',
url: 'admin/plugins/manage?action=requirements&name=plugin',
dataType: 'json'
});
expect(swal).toBeCalledWith({
text: 'admin.noDependenciesNotice',
type: 'warning',
showCancelButton: true
});
expect(fetch.mock.calls.length).toBe(1);
await enablePlugin('plugin');
expect(fetch).toBeCalledWith({
type: 'POST',
url: 'admin/plugins/manage?action=enable&name=plugin',
dataType: 'json'
});
expect(toastr.warning).not.toBeCalled();
expect(swal).not.toBeCalledWith({ type: 'warning' });
expect(toastr.success).toBeCalledWith('success');
expect(reloadTable).toBeCalledWith(null, false);
await enablePlugin('plugin');
expect(toastr.warning).toBeCalledWith('warning');
expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' });
await enablePlugin('plugin');
expect(showAjaxError).toBeCalled();
@ -476,12 +500,12 @@ describe('tests for "plugins" module', () => {
url: 'admin/plugins/manage?action=disable&name=plugin',
dataType: 'json'
});
expect(toastr.warning).not.toBeCalled();
expect(swal).not.toBeCalledWith({ type: 'warning' });
expect(toastr.success).toBeCalledWith('success');
expect(reloadTable).toBeCalledWith(null, false);
await disablePlugin('plugin');
expect(toastr.warning).toBeCalledWith('warning');
expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' });
await disablePlugin('plugin');
expect(showAjaxError).toBeCalled();
@ -529,12 +553,12 @@ describe('tests for "plugins" module', () => {
url: 'admin/plugins/manage?action=delete&name=plugin',
dataType: 'json'
});
expect(toastr.warning).not.toBeCalled();
expect(swal).not.toBeCalledWith({ type: 'warning' });
expect(toastr.success).toBeCalledWith('success');
expect(reloadTable).toBeCalledWith(null, false);
await deletePlugin('plugin');
expect(toastr.warning).toBeCalledWith('warning');
expect(swal).toBeCalledWith({ type: 'warning', html: 'warning' });
await deletePlugin('plugin');
expect(showAjaxError).toBeCalled();

View File

@ -83,7 +83,33 @@ class PluginControllerTest extends TestCase
'msg' => trans('admin.invalid-action')
]);
// Enable a plugin with unsatisfied dependencies
app('plugins')->getPlugin('avatar-api')->setRequirements([
'blessing-skin-server' => '^3.4.0',
'example-plugin' => '^6.6.6',
'whatever' => '^1.0.0'
]);
app('plugins')->enable('example-plugin');
$this->post('/admin/plugins/manage', [
'name' => 'avatar-api',
'action' => 'enable'
])->seeJson([
'errno' => 1,
'msg' => sprintf(
'<p>%s</p><ul><li>%s</li><li>%s</li></ul>',
trans('admin.plugins.operations.unsatisfied.notice'),
trans('admin.plugins.operations.unsatisfied.version', [
'name' => "<code>example-plugin</code>",
'constraint' => "<code>^6.6.6</code>"
]),
trans('admin.plugins.operations.unsatisfied.disabled', [
'name' => "<code>whatever</code>"
])
)
]);
// Enable a plugin
app('plugins')->getPlugin('avatar-api')->setRequirements([]);
$this->expectsEvents(App\Events\PluginWasEnabled::class);
$this->post('/admin/plugins/manage', [
'name' => 'avatar-api',
@ -135,6 +161,7 @@ class PluginControllerTest extends TestCase
'url',
'namespace',
'status',
'dependencies',
'operations' => ['enabled', 'hasConfigView']
]]
]);