Merge pull request #219 from nasa/open213

[Mobile] Don't move DOM nodes
This commit is contained in:
akhenry 2015-11-05 12:09:22 -08:00
commit a356e01b14
2 changed files with 26 additions and 21 deletions

View File

@ -85,7 +85,7 @@ define(
function link(scope, element, attrs, ctrl, transclude) {
if (deviceMatches(attrs.mctDevice)) {
transclude(function (clone) {
element.parent().append(clone);
element.replaceWith(clone);
});
}
}

View File

@ -26,13 +26,12 @@ define(
function (MCTDevice) {
"use strict";
var JQLITE_METHODS = [ 'parent', 'append' ];
var JQLITE_METHODS = [ 'replaceWith' ];
describe("The mct-device directive", function () {
var mockAgentService,
mockTransclude,
mockElement,
mockParent,
mockClone,
testAttrs,
directive;
@ -48,11 +47,8 @@ define(
);
mockTransclude = jasmine.createSpy("$transclude");
mockElement = jasmine.createSpyObj(name, JQLITE_METHODS);
mockParent = jasmine.createSpyObj(name, JQLITE_METHODS);
mockClone = jasmine.createSpyObj(name, JQLITE_METHODS);
mockElement.parent.andReturn(mockParent);
mockTransclude.andCallFake(function (fn) {
fn(mockClone);
});
@ -65,6 +61,15 @@ define(
directive = new MCTDevice(mockAgentService);
});
function expectInclusion() {
expect(mockElement.replaceWith)
.toHaveBeenCalledWith(mockClone);
}
function expectExclusion() {
expect(mockElement.replaceWith).not.toHaveBeenCalled();
}
it("is applicable at the attribute level", function () {
expect(directive.restrict).toEqual("A");
});
@ -80,54 +85,54 @@ define(
it("restricts element inclusion for mobile devices", function () {
testAttrs.mctDevice = "mobile";
link();
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isMobile.andReturn(true);
link();
expect(mockParent.append).toHaveBeenCalledWith(mockClone);
expectInclusion();
});
it("restricts element inclusion for tablet devices", function () {
testAttrs.mctDevice = "tablet";
mockAgentService.isMobile.andReturn(true);
link();
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isTablet.andReturn(true);
link();
expect(mockParent.append).toHaveBeenCalledWith(mockClone);
expectInclusion();
});
it("restricts element inclusion for phone devices", function () {
testAttrs.mctDevice = "phone";
mockAgentService.isMobile.andReturn(true);
link();
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isPhone.andReturn(true);
link();
expect(mockParent.append).toHaveBeenCalledWith(mockClone);
expectInclusion();
});
it("restricts element inclusion for desktop devices", function () {
testAttrs.mctDevice = "desktop";
mockAgentService.isMobile.andReturn(true);
link();
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isMobile.andReturn(false);
link();
expect(mockParent.append).toHaveBeenCalledWith(mockClone);
expectInclusion();
});
it("restricts element inclusion for portrait orientation", function () {
testAttrs.mctDevice = "portrait";
link();
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isPortrait.andReturn(true);
link();
expect(mockParent.append).toHaveBeenCalledWith(mockClone);
expectInclusion();
});
it("restricts element inclusion for landscape orientation", function () {
@ -135,11 +140,11 @@ define(
mockAgentService.isLandscape.andReturn(false);
mockAgentService.isPortrait.andReturn(true);
link();
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isLandscape.andReturn(true);
link();
expect(mockParent.append).toHaveBeenCalledWith(mockClone);
expectInclusion();
});
it("allows multiple device characteristics to be requested", function () {
@ -148,17 +153,17 @@ define(
testAttrs.mctDevice = "portrait mobile";
link();
// Neither portrait nor mobile, not called
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isPortrait.andReturn(true);
link();
// Was portrait, but not mobile, so no
expect(mockParent.append).not.toHaveBeenCalled();
expectExclusion();
mockAgentService.isMobile.andReturn(true);
link();
expect(mockParent.append).toHaveBeenCalledWith(mockClone);
expectInclusion();
});
});
}