mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Refactoring admin table panels infrastructure
Updating enterprise index
This commit is contained in:
@@ -38,7 +38,7 @@ describe "indexPanelCtrl", ->
|
||||
expect(scope.saving).toBe false
|
||||
|
||||
it "emits an 'enterprise:updated' event", ->
|
||||
expect(scope.$emit).toHaveBeenCalledWith("enterprise:updated")
|
||||
expect(scope.$emit).toHaveBeenCalledWith("enterprise:updated", scope.enterprise)
|
||||
|
||||
describe "when the save is unsuccessful", ->
|
||||
beforeEach inject ($rootScope) ->
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
describe "PanelRow directive", ->
|
||||
Panels = null
|
||||
element = null
|
||||
directiveScope = null
|
||||
panelScope = null
|
||||
rowScope = null
|
||||
|
||||
beforeEach ->
|
||||
module 'admin.indexUtils'
|
||||
@@ -13,24 +14,27 @@ describe "PanelRow directive", ->
|
||||
Panels = _Panels_
|
||||
$templateCache.put 'admin/panel.html', '<span>{{ template }}</span>'
|
||||
# Declare the directive HTML.
|
||||
element = angular.element('<div class="panel-row" object="{id: \'12\'}" panels="{ panel1: \'template\'}"></div>')
|
||||
element = angular.element('<table><tbody class="panel-toggle-row"><tr class="panel-row" object="{id: \'12\'}" panels="{ panel1: \'template\'}"></tr></tbody><table>')
|
||||
# Define the root scope.
|
||||
scope = $rootScope
|
||||
# Compile and digest the directive.
|
||||
$compile(element) scope
|
||||
scope.$digest()
|
||||
|
||||
directiveScope = element.find('span').scope()
|
||||
rowScope = element.find('tbody').isolateScope()
|
||||
panelScope = element.find('tr').isolateScope()
|
||||
return
|
||||
|
||||
describe "initialisation", ->
|
||||
it "registers the scope with the panels service", ->
|
||||
expect(Panels.panels[12]).toEqual directiveScope
|
||||
it "registers a listener on the row scope", ->
|
||||
expect(rowScope.$$listeners["selection:changed"].length).toEqual 1
|
||||
|
||||
describe "setting the selected panel", ->
|
||||
describe "when a select event is triggered on the row scope", ->
|
||||
beforeEach ->
|
||||
directiveScope.setSelected('panel1')
|
||||
rowScope.$broadcast('selection:changed', 'panel1')
|
||||
|
||||
it 'updates the active template on the scope', ->
|
||||
panelScope.$digest()
|
||||
expect(panelScope.template).toEqual "admin/panels/template.html"
|
||||
expect(element.find('span').html()).toEqual "admin/panels/template.html"
|
||||
return
|
||||
|
||||
@@ -8,45 +8,110 @@ describe "Panels service", ->
|
||||
Panels = _Panels_
|
||||
|
||||
describe "registering panels", ->
|
||||
it "adds the panel provided scope to @panelsm indexed by the provided id", ->
|
||||
Panels.register(23, { some: 'scope'} )
|
||||
expect(Panels.panels[23]).toEqual { some: 'scope' }
|
||||
ctrl1 = ctrl2 = null
|
||||
beforeEach ->
|
||||
ctrl1 = jasmine.createSpyObj('ctrl', ['select'])
|
||||
ctrl2 = jasmine.createSpyObj('ctrl', ['select'])
|
||||
|
||||
it "ignores the input if id or scope are null", ->
|
||||
Panels.register(null, { some: 'scope'} )
|
||||
Panels.register(23, null)
|
||||
expect(Panels.panels).toEqual { }
|
||||
it "adds the panels controller, object and selection to @panels", ->
|
||||
Panels.register(ctrl1, { name: "obj1"}, "panel1")
|
||||
Panels.register(ctrl2, { name: "obj2"})
|
||||
expect(Panels.panels).toEqual [
|
||||
{ ctrl: ctrl1, object: { name: "obj1"}, selected: "panel1" },
|
||||
{ ctrl: ctrl2, object: { name: "obj2"}, selected: null }
|
||||
]
|
||||
|
||||
it "call select on the controller if a selection is provided", ->
|
||||
Panels.register(ctrl1, { name: "obj1"}, "panel1")
|
||||
Panels.register(ctrl2, { name: "obj2"})
|
||||
expect(ctrl1.select.calls.count()).toEqual 1
|
||||
expect(ctrl2.select.calls.count()).toEqual 0
|
||||
|
||||
it "ignores the input if object or ctrl are null", ->
|
||||
Panels.register(ctrl1, null)
|
||||
Panels.register(null, { name: "obj2"})
|
||||
expect(Panels.panels).toEqual []
|
||||
|
||||
describe "toggling a panel", ->
|
||||
scopeMock = null
|
||||
panelMock = ctrlMock = objMock = null
|
||||
|
||||
beforeEach ->
|
||||
scopeMock =
|
||||
open: jasmine.createSpy('open')
|
||||
close: jasmine.createSpy('close')
|
||||
setSelected: jasmine.createSpy('setSelected')
|
||||
Panels.panels = { '12': scopeMock }
|
||||
ctrlMock = jasmine.createSpyObj('ctrl', ['select'])
|
||||
panelMock = { ctrl: ctrlMock }
|
||||
spyOn(Panels, "findPanelByObject").and.returnValue(panelMock)
|
||||
|
||||
describe "when no panel is currently selected", ->
|
||||
beforeEach ->
|
||||
scopeMock.getSelected = jasmine.createSpy('getSelected').and.returnValue(null)
|
||||
Panels.toggle(12, 'panel_name')
|
||||
panelMock.selected = null
|
||||
|
||||
it "calls #open on the scope", ->
|
||||
expect(scopeMock.open).toHaveBeenCalledWith('panel_name')
|
||||
describe "when no state is provided", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name')
|
||||
|
||||
describe "when #toggle is called for the currently selected panel", ->
|
||||
it "selects the named panel", ->
|
||||
expect(panelMock.selected).toEqual 'panel_name'
|
||||
expect(ctrlMock.select).toHaveBeenCalledWith('panel_name')
|
||||
|
||||
describe "when the state given is 'open'", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name', "open")
|
||||
|
||||
it "selects the named panel", ->
|
||||
expect(panelMock.selected).toEqual 'panel_name'
|
||||
expect(ctrlMock.select).toHaveBeenCalledWith('panel_name')
|
||||
|
||||
describe "when the state given is 'closed'", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name', "closed")
|
||||
|
||||
it "does not select the named panel", ->
|
||||
expect(panelMock.selected).toEqual null
|
||||
expect(ctrlMock.select).not.toHaveBeenCalledWith('panel_name')
|
||||
|
||||
describe "when the currently selected panel matches the named panel", ->
|
||||
beforeEach ->
|
||||
scopeMock.getSelected = jasmine.createSpy('getSelected').and.returnValue('panel_name')
|
||||
Panels.toggle(12, 'panel_name')
|
||||
panelMock.selected = 'panel_name'
|
||||
|
||||
it "calls #close on the scope", ->
|
||||
expect(scopeMock.close).toHaveBeenCalled()
|
||||
describe "when no state is provided", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name')
|
||||
|
||||
describe "when #toggle is called for a different panel", ->
|
||||
it "de-selects the named panel", ->
|
||||
expect(panelMock.selected).toEqual null
|
||||
expect(ctrlMock.select).toHaveBeenCalledWith(null)
|
||||
|
||||
describe "when the state given is 'open'", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name', "open")
|
||||
|
||||
it "keeps the the named panel selected, but does not call select on the controller", ->
|
||||
expect(panelMock.selected).toEqual 'panel_name'
|
||||
expect(ctrlMock.select).not.toHaveBeenCalledWith('panel_name')
|
||||
|
||||
describe "when the state given is 'closed'", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name', "closed")
|
||||
|
||||
it "de-selects the named panel", ->
|
||||
expect(panelMock.selected).toEqual null
|
||||
expect(ctrlMock.select).not.toHaveBeenCalledWith('panel_name')
|
||||
|
||||
describe "when the currently selected panel does not match the requested panel", ->
|
||||
beforeEach ->
|
||||
scopeMock.getSelected = jasmine.createSpy('getSelected').and.returnValue('some_other_panel_name')
|
||||
Panels.toggle(12, 'panel_name')
|
||||
panelMock.selected = 'some_other_panel'
|
||||
|
||||
it "calls #setSelected on the scope", ->
|
||||
expect(scopeMock.setSelected).toHaveBeenCalledWith('panel_name')
|
||||
describe "when no state is provided", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name')
|
||||
|
||||
it "selects the named panel", ->
|
||||
expect(panelMock.selected).toEqual 'panel_name'
|
||||
expect(ctrlMock.select).toHaveBeenCalledWith('panel_name')
|
||||
|
||||
describe "when the state given is 'open'", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name', "open")
|
||||
|
||||
it "selects the named panel", ->
|
||||
expect(panelMock.selected).toEqual 'panel_name'
|
||||
expect(ctrlMock.select).toHaveBeenCalledWith('panel_name')
|
||||
|
||||
describe "when the state given is 'closed'", ->
|
||||
beforeEach -> Panels.toggle({some: "object"}, 'panel_name', "closed")
|
||||
|
||||
it "keeps the currently selected panel selected, ie. does nothing", ->
|
||||
expect(panelMock.selected).toEqual "some_other_panel"
|
||||
expect(ctrlMock.select).not.toHaveBeenCalledWith('panel_name')
|
||||
expect(ctrlMock.select).not.toHaveBeenCalledWith('some_other_panel')
|
||||
|
||||
Reference in New Issue
Block a user