-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from fragsalat/else-fix
fix(if-else): Fixed missing binding context when rendering else block
- Loading branch information
Showing
3 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import './setup'; | ||
import {ViewSlot} from 'aurelia-templating'; | ||
import {If} from '../src/if'; | ||
import {Else} from '../src/else'; | ||
|
||
class ViewMock { | ||
bind() {} | ||
unbind() {} | ||
} | ||
|
||
describe('else', () => { | ||
let ifViewSlot, elseViewSlot, ifVm, elseVm, viewFactory; | ||
|
||
beforeEach(() => { | ||
const fragment = document.createDocumentFragment(); | ||
const ifNode = document.createElement('div'); | ||
const elseNode = document.createElement('div'); | ||
fragment.appendChild(ifNode); | ||
fragment.appendChild(elseNode); | ||
ifViewSlot = new ViewSlot(ifNode, true); | ||
elseViewSlot = new ViewSlot(elseNode, true); | ||
ifVm = new If(viewFactory, ifViewSlot); | ||
ifVm.view = new ViewMock(); | ||
ifNode.au = {if: {viewModel: ifVm}}; | ||
|
||
elseVm = new Else(viewFactory, elseViewSlot); | ||
elseVm.view = new ViewMock(); | ||
}); | ||
|
||
it('should render when initial condition is false', () => { | ||
ifVm.showing = false; | ||
elseVm.showing = false; | ||
|
||
spyOn(ifViewSlot, 'add'); | ||
spyOn(ifVm.view, 'bind'); | ||
spyOn(elseViewSlot, 'add'); | ||
spyOn(elseVm.view, 'bind'); | ||
|
||
ifVm.condition = false; | ||
ifVm.bind(); | ||
elseVm.bind(); | ||
// Else should be shown now | ||
expect(ifVm.showing).toBeFalsy(); | ||
expect(ifViewSlot.add).not.toHaveBeenCalled(); | ||
expect(ifVm.view.bind).not.toHaveBeenCalled(); | ||
expect(elseVm.showing).toBeTruthy(); | ||
expect(elseViewSlot.add).toHaveBeenCalledTimes(1); | ||
expect(elseVm.view.bind).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not render when initial condition is true', () => { | ||
ifVm.showing = false; | ||
elseVm.showing = false; | ||
|
||
spyOn(elseViewSlot, 'add'); | ||
spyOn(elseVm.view, 'bind'); | ||
spyOn(ifViewSlot, 'add'); | ||
spyOn(ifVm.view, 'bind'); | ||
|
||
ifVm.condition = true; | ||
ifVm.bind(); | ||
elseVm.bind(); | ||
// If should be shown now | ||
expect(elseVm.showing).toBeFalsy(); | ||
expect(elseViewSlot.add).not.toHaveBeenCalled(); // Was not added yet | ||
expect(elseVm.view.bind).not.toHaveBeenCalled(); // Was not added yet | ||
expect(ifVm.showing).toBeTruthy(); | ||
expect(ifViewSlot.add).toHaveBeenCalledTimes(1); | ||
expect(ifVm.view.bind).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should render when condition changes to false', () => { | ||
ifVm.showing = false; | ||
elseVm.showing = false; | ||
|
||
spyOn(ifViewSlot, 'add'); | ||
spyOn(ifVm.view, 'bind'); | ||
spyOn(ifViewSlot, 'remove'); | ||
spyOn(ifVm.view, 'unbind'); | ||
spyOn(elseViewSlot, 'add'); | ||
spyOn(elseVm.view, 'bind'); | ||
|
||
ifVm.condition = true; | ||
ifVm.bind(); | ||
elseVm.bind(); | ||
// Nothing should happen yet since else is not bound yet | ||
expect(ifVm.showing).toBeTruthy(); | ||
expect(ifViewSlot.add).toHaveBeenCalledTimes(1); | ||
expect(ifVm.view.bind).toHaveBeenCalledTimes(1); | ||
expect(elseVm.showing).toBeFalsy(); | ||
expect(elseViewSlot.add).not.toHaveBeenCalled(); | ||
expect(elseVm.view.bind).not.toHaveBeenCalled(); | ||
|
||
ifVm.condition = false; | ||
ifVm.conditionChanged(false); | ||
|
||
// Else should be shown now and if should be removed | ||
expect(ifVm.showing).toBeFalsy(); | ||
expect(ifViewSlot.remove).toHaveBeenCalledTimes(1); | ||
expect(ifVm.view.unbind).toHaveBeenCalledTimes(1); | ||
expect(elseVm.showing).toBeTruthy(); | ||
expect(elseViewSlot.add).toHaveBeenCalledTimes(1); | ||
expect(elseVm.view.bind).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should render when condition changes to true', () => { | ||
ifVm.showing = false; | ||
elseVm.showing = false; | ||
|
||
spyOn(ifViewSlot, 'add'); | ||
spyOn(ifVm.view, 'bind'); | ||
spyOn(elseViewSlot, 'add'); | ||
spyOn(elseVm.view, 'bind'); | ||
spyOn(elseViewSlot, 'remove'); | ||
spyOn(elseVm.view, 'unbind'); | ||
|
||
ifVm.condition = false; | ||
ifVm.bind(); | ||
elseVm.bind(); | ||
// Nothing should happen yet since else is not bound yet | ||
expect(ifVm.showing).toBeFalsy(); | ||
expect(ifViewSlot.add).not.toHaveBeenCalled(); | ||
expect(ifVm.view.bind).not.toHaveBeenCalled(); | ||
expect(elseVm.showing).toBeTruthy(); | ||
expect(elseViewSlot.add).toHaveBeenCalledTimes(1); | ||
expect(elseVm.view.bind).toHaveBeenCalledTimes(1); | ||
|
||
ifVm.condition = true; | ||
ifVm.conditionChanged(true); | ||
|
||
// Else should be shown now and if should be removed | ||
expect(ifVm.showing).toBeTruthy(); | ||
expect(ifViewSlot.add).toHaveBeenCalledTimes(1); | ||
expect(ifVm.view.bind).toHaveBeenCalledTimes(1); | ||
expect(elseVm.showing).toBeFalsy(); | ||
expect(elseViewSlot.remove).toHaveBeenCalledTimes(1); | ||
expect(elseVm.view.unbind).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |