From b4e8b7e8c9cd11c6b5948359d88047e7039c2753 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 29 Jun 2018 18:26:48 +0800 Subject: [PATCH] Update tests of plugins --- .../assets/src/js/__tests__/admin.test.js | 36 +++++++++++++++---- tests/PluginControllerTest.php | 27 ++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/resources/assets/src/js/__tests__/admin.test.js b/resources/assets/src/js/__tests__/admin.test.js index e47e02bd..16bb68a9 100644 --- a/resources/assets/src/js/__tests__/admin.test.js +++ b/resources/assets/src/js/__tests__/admin.test.js @@ -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(); diff --git a/tests/PluginControllerTest.php b/tests/PluginControllerTest.php index 1e6fc9f6..bca09319 100644 --- a/tests/PluginControllerTest.php +++ b/tests/PluginControllerTest.php @@ -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( + '

%s

', + trans('admin.plugins.operations.unsatisfied.notice'), + trans('admin.plugins.operations.unsatisfied.version', [ + 'name' => "example-plugin", + 'constraint' => "^6.6.6" + ]), + trans('admin.plugins.operations.unsatisfied.disabled', [ + 'name' => "whatever" + ]) + ) + ]); + // 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'] ]] ]);