Skip to content

Commit

Permalink
Fix attaching the model to new partial with same URL,
Browse files Browse the repository at this point in the history
Fix race condition between dom-bind and imported-template in tests

#25
https://github.com/Starcounter/RebelsLounge/issues/97
  • Loading branch information
tomalec committed Mar 30, 2017
1 parent 0a4700b commit f5b5390
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 2 deletions.
2 changes: 2 additions & 0 deletions starcounter-include.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@
}
this._forceLayoutChange = true;
this._lastHtml = html;
} else if (this.template.model != this.partial) {
this.template.model = this.partial
}
};
LauncherPartialPrototype._compositionChanged = function(composition) {
Expand Down
22 changes: 22 additions & 0 deletions test/partialAttribute/given_in_HTML.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@
testAllAttributes();
});

describe('changed after element was attached and stamped to the new object, with the same Html, model should be attached immediately', function (done) {
beforeEach(function(done){
scInclude = fixture('full');
importedTemplate = scInclude.querySelector_template_for_buggy_polyfill();

importedTemplate.addEventListener('stamping', function afterAttached(){
importedTemplate.removeEventListener('stamping', afterAttached);
setTimeout(function(){
let oldModel = scInclude.partial;
scInclude.setAttribute('partial', JSON.stringify(oldModel));
done();
});
});
});
it('should attach `.Html` property as content attribute for `imported-template`', function () {
expect(importedTemplate.getAttribute("content")).to.equal(scInclude.partial.Html);
});
it('should attach partial model to `imported-template` immediately', function () {
expect(importedTemplate.model).to.equal(scInclude.partial);
});
});

describe('set after element was detached', function(){
beforeEach(function(){
scInclude = fixture('nothing');
Expand Down
26 changes: 25 additions & 1 deletion test/partialAttribute/given_in_JS.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@
testAllAttributes();
});

describe('changed after element was attached and stamped to the new object, with the same Html, model should be attached immediately', function (done) {
beforeEach(function(done){
scInclude = fixture('full');
importedTemplate = scInclude.querySelector_template_for_buggy_polyfill();

importedTemplate.addEventListener('stamping', function afterAttached(){
importedTemplate.removeEventListener('stamping', afterAttached);
setTimeout(function(){
let oldModel = scInclude.partial;
let newModel = clone(oldModel);
newModel.doesItWork = 'still works!';
scInclude.partial = newModel;
done();
});
});
});
it('should attach `.Html` property as content attribute for `imported-template`', function () {
expect(importedTemplate.getAttribute("content")).to.equal(scInclude.partial.Html);
});
it('should attach partial model to `imported-template` immediately', function () {
expect(importedTemplate.model).to.equal(scInclude.partial);
});
});

describe('set after element was detached', function(){
beforeEach(function(){
scInclude = fixture('nothing');
Expand All @@ -115,7 +139,7 @@
});
testAllAttributes();
});
describe('chnaged after element was detached', function(){
describe('changed after element was detached', function(){
beforeEach(function(done){
scInclude = fixture('old');
previousModel = scInclude.partial;
Expand Down
16 changes: 16 additions & 0 deletions test/partialAttribute/nested-html/given_in_HTML.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@
});
});

it('if value was changed after element was attached and stamped to the new object, with the same Html, model should be attached immediately', function (done) {
scInclude = fixture('nested-html');
let importedTemplate = scInclude.querySelector_template_for_buggy_polyfill();

importedTemplate.addEventListener('stamping', function afterAttached(){
importedTemplate.removeEventListener('stamping', afterAttached);
setTimeout(function(){
let oldModel = scInclude.partial;
scInclude.setAttribute('partial', JSON.stringify(oldModel));

expect(importedTemplate.getAttribute("content")).to.equal('/sc/htmlmerger?scope1=scope1Html&scope2=scope2Html%3B%2C%2F%3F%3A%40%26%3D%2B%24&scope3=');
expect(importedTemplate.model).to.equal(scInclude.partial);
done();
});
});
});

it('if value was set after element was detached (model not yet attached)', function () {
scInclude = fixture('nothing');
Expand Down
19 changes: 19 additions & 0 deletions test/partialAttribute/nested-html/given_in_JS.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@
});
});

it('if value was changed after element was attached and stamped to the new object, with the same Html, model should be attached immediately', function (done) {
scInclude = fixture('nested-html');
let importedTemplate = scInclude.querySelector_template_for_buggy_polyfill();

importedTemplate.addEventListener('stamping', function afterAttached(){
importedTemplate.removeEventListener('stamping', afterAttached);
setTimeout(function(){
let oldModel = scInclude.partial;
let newModel = clone(oldModel);
newModel.scope1.doesItWork = 'still works!';
scInclude.partial = newModel;

expect(importedTemplate.getAttribute("content")).to.equal('/sc/htmlmerger?scope1=scope1Html&scope2=scope2Html&scope3=');
expect(importedTemplate.model).to.equal(scInclude.partial);
done();
});
});
});

it('if value was set after element was detached (model not yet attached)', function () {
scInclude = fixture('nothing');
scInclude.parentElement.removeChild(scInclude);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
});
});

xit('should attach new `.Html` property as content attribute for `<imported-template>`', function () {
it('should attach new `.Html` property as content attribute for `<imported-template>`', function () {
expect(importedTemplate.getAttribute("content")).to.equal('/sc/htmlmerger?scopeA=scopeAHtml&scopeB=scopeBHtml');
});
it('should NOT attach new partial model to `imported-template` immediately', function () {
Expand All @@ -121,6 +121,38 @@
});
});

describe('after `dom-bind`` changed the partial property to new one with the same Html', function () {
var changedPartialWithSameHtml;
beforeEach(function(done) {
// IDEA: promisify juicy-html
importedTemplate.addEventListener('stamping', function onceStamped(){
importedTemplate.removeEventListener('stamping', onceStamped);
changedPartialWithSameHtml = {
'scope1': {
'Html': 'scope1Html',
'doesItWork': 'still works!'
},
'scope2': {
'Html': 'scope2Html;,/?:@&=+$',
'doesItWork': 'still works!'
},
'scope3': {
'doesItWork': 'still works!'
}
};
domBind.set('model.Page', changedPartialWithSameHtml);
done();
});
});

it('should attach new `.Html` property as content attribute for `<imported-template>`', function () {
expect(importedTemplate.getAttribute("content")).to.equal('/sc/htmlmerger?scope1=scope1Html&scope2=scope2Html%3B%2C%2F%3F%3A%40%26%3D%2B%24&scope3=');
});
it('should attach new partial model to `imported-template` immediately', function () {
expect(importedTemplate.model).to.equal(changedPartialWithSameHtml);
});
});

describe('after `dom-bind`` changed the inner `Html` property', function () {
beforeEach(function() {
domBind.set('model.Page.scope1.Html', 'changedHtml');
Expand Down
23 changes: 23 additions & 0 deletions test/partialAttribute/stamped_from_polymer_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@
});
});

describe('after `dom-bind`` changed the partial property to new one with the same Html', function () {
var changedPartialWithSameHtml;
beforeEach(function(done) {
// IDEA: promisify juicy-html
importedTemplate.addEventListener('stamping', function onceStamped(){
importedTemplate.removeEventListener('stamping', onceStamped);
changedPartialWithSameHtml = {
Html: "../old_templateToInclude.html",
doesItWork: "works!"
};
domBind.set('model.Page', changedPartialWithSameHtml);
done();
});
});

it('should attach new `.Html` property as content attribute for `<imported-template>`', function () {
expect(importedTemplate.getAttribute("content")).to.equal('../old_templateToInclude.html');
});
it('should attach new partial model to `imported-template` immediately', function () {
expect(importedTemplate.model).to.equal(changedPartialWithSameHtml);
});
});

describe('after `dom-bind`` changed the partial.Html property', function() {
beforeEach(function(done) {
importedTemplate.addEventListener('stamping', function onceStamped(){
Expand Down

0 comments on commit f5b5390

Please sign in to comment.