Skip to content

Instantly share code, notes, and snippets.

@chestozo
Created January 23, 2014 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chestozo/8583693 to your computer and use it in GitHub Desktop.
Save chestozo/8583693 to your computer and use it in GitHub Desktop.
noscript bindModels test case
describe('redraw old view when model was invalidated', function() {
// https://github.com/yandex-ui/noscript/pull/192#issuecomment-33148362
// После починки _unbindModels перестало работать обновление view при изменении модели.
// Дело тут в том, что view подписана на своим модели. Модель поменялась - view перерисовалась (model ns-model-changed -> view invalidate).
// Когда мы починили отписку view от модели (во время _hide) изменения модели больше не будут услышаны view (что, как бы, хорошо и by design).
// Но тогда, во время update-а надо проверять, что все модели view валидны. И если нет - обновлять view.
// Итог: не отписываем view от моделей
var goToPage = function(app, params, callback) {
var layout = ns.layout.page('app', params);
return new ns.Update(app, layout, params).start().done(function() { callback(); });
};
beforeEach(function() {
this.spies = {};
ns.View.define('app');
ns.View.define('view', { models: [ 'model' ] });
// Модель и сразу два экземляра создаём заранее
ns.Model.define('model', { params: { id: null } });
this.model1 = ns.Model.get('model', { id: 1 }).setData({ data: 1 });
this.model2 = ns.Model.get('model', { id: 2 }).setData({ data: 2 });
ns.layout.define('app', {
'app': {
'box@': 'view'
}
});
this.appView = ns.View.create('app');
});
afterEach(function() {
for (var spyName in this.spies) {
this.spies[spyName].restore();
}
});
it('do not bind twice to model changed after show - hide - show cicle', function(done) {
var spies = this.spies;
var model1 = this.model1;
var app = this.appView;
var view1;
// Показываем страницы: 1 - 2 - 1 - 2
// view1 показывалось 2 раза.
// Надо проверить, что не будет двойного invalidate на ns-model-changed
goToPage(app, { id: 1 }, function() {
view1 = app.views.box.views['view=view&id=1'];
spies.view1Invalidate = sinon.spy(view1, 'invalidate');
goToPage(app, { id: 2 }, function() {
goToPage(app, { id: 1 }, function() {
goToPage(app, { id: 2 }, function() {
model1.setData({ id: 1, changed: true });
expect(spies.view1Invalidate.callCount).to.be.eql(1);
done();
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment