Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the listener_ to be called with the abstract model interface. #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/helper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ helper.DataLayerHelper = function(dataLayer, opt_listener, opt_listenToPast) {

/**
* The listener to notify of changes to the dataLayer.
* @type {function(!Object, !Object)}
* @type {function(!Object)}
* @private
*/
this.listener_ = opt_listener || function() {};
Expand Down Expand Up @@ -199,7 +199,7 @@ helper.DataLayerHelper.prototype.processStates_ =
}
if (!opt_skipListener) {
this.executingListener_ = true;
this.listener_(this.model_, update);
this.listener_.call(this.abstractModelInterface_, update);
this.executingListener_ = false;
}
}
Expand Down
12 changes: 6 additions & 6 deletions test/integration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ test('Basic Operations', function() {
ok(helper.get('two') === undefined);

dataLayer.push({one: 1, two: 2});
assertCallback([{one: 1, two: 2}, {one: 1, two: 2}]);
assertCallback([{one: 1, two: 2}]);
ok(helper.get('one') === 1);
ok(helper.get('two') === 2);

dataLayer.push({two: 3});
assertCallback([{one: 1, two: 3}, {two: 3}]);
assertCallback([{two: 3}]);
ok(helper.get('one') === 1);
ok(helper.get('two') === 3);

dataLayer.push({two: 2});
assertCallback([{one: 1, two: 2}, {two: 2}]);
assertCallback([{two: 2}]);
ok(helper.get('one') === 1);
ok(helper.get('two') === 2);

dataLayer.push({one: {three: 3}});
assertCallback([{one: {three: 3}, two: 2}, {one: {three: 3}}]);
assertCallback([{one: {three: 3}}]);
deepEqual(helper.get('one'), {three: 3});
ok(helper.get('two') === 2);

dataLayer.push({one: {four: 4}});
assertCallback([{one: {three: 3, four: 4}, two: 2}, {one: {four: 4}}]);
assertCallback([{one: {four: 4}}]);
deepEqual(helper.get('one'), {three: 3, four: 4});
ok(helper.get('one.four') === 4);
ok(helper.get('two') === 2);
Expand All @@ -92,7 +92,7 @@ test('Basic Operations', function() {
ok(helper.get('two') === 2);

dataLayer.push({five: 5});
assertCallback([{one: {three: 3, four: 4}, two: 2, five: 5}, {five: 5}]);
assertCallback([{five: 5}]);
equal(dataLayer.length, 2);
ok(helper.get('one.four') === 4);
ok(helper.get('five') === 5);
Expand Down
53 changes: 26 additions & 27 deletions test/processStates_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function assertProcessStates(states, expectedModel, expectedListenerCalls) {
this.unprocessed_ = [];
this.executingListener_ = false;
this.listenerCalls_ = [];
var that = this;
this.listener_ = function() {
this.listenerCalls_.push(
that.listenerCalls_.push(
jQuery.extend(true, [], [].slice.call(arguments, 0)));
};
this.abstractModelInterface_ = helper.buildAbstractModelInterface_(this);
Expand All @@ -57,11 +58,9 @@ function assertProcessStates(states, expectedModel, expectedListenerCalls) {

test('processStates', function() {
assertProcessStates([], {}, []);
assertProcessStates([{a: 1}], {a: 1}, [[{a: 1}, {a: 1}]]);
assertProcessStates([{a: 1}, {a: 2}], {a: 2},
[[{a: 1}, {a: 1}], [{a: 2}, {a: 2}]]);
assertProcessStates([{'a.b': 1}], {a: {b: 1}},
[[{a: {b: 1}}, {'a.b': 1}]]);
assertProcessStates([{a: 1}], {a: 1}, [[{a: 1}]]);
assertProcessStates([{a: 1}, {a: 2}], {a: 2}, [[{a: 1}], [{a: 2}]]);
assertProcessStates([{'a.b': 1}], {a: {b: 1}}, [[{'a.b': 1}]]);
});

test('processStates_customMethods', function() {
Expand All @@ -70,19 +69,19 @@ test('processStates_customMethods', function() {
assertProcessStates(
[{a: 0}, customMethod],
{a: 1},
[[{a: 0}, {a: 0}], [{a: 1}, customMethod]]);
[[{a: 0}], [customMethod]]);

customMethod = function() { this.set('b', 'one'); };
assertProcessStates(
[customMethod],
{b: 'one'},
[[{b: 'one'}, customMethod]]);
[[customMethod]]);

customMethod = function() { this.set('c.d', [3]); };
assertProcessStates(
[customMethod],
{c: {d: [3]}},
[[{c: {d: [3]}}, customMethod]]);
[[customMethod]]);

var customMethod = function() {
var a = this.get('a');
Expand All @@ -92,8 +91,8 @@ test('processStates_customMethods', function() {
[{a: 1, b: 2}, customMethod],
{a: 1, b: 2},
[
[{a: 1, b: 2}, {a: 1, b: 2}],
[{a: 1, b: 2}, customMethod]
[{a: 1, b: 2}],
[customMethod]
]);

customMethod = function() {
Expand All @@ -104,8 +103,8 @@ test('processStates_customMethods', function() {
[{a: {b: 2}}, customMethod],
{a: {b: 2}},
[
[{a: {b: 2}}, {a: {b: 2}}],
[{a: {b: 2}}, customMethod]
[{a: {b: 2}}],
[customMethod]
]);

customMethod = function() {
Expand All @@ -116,8 +115,8 @@ test('processStates_customMethods', function() {
[{a: {b: {c: [3]}}}, customMethod],
{a: {b: {c: [3]}}},
[
[{a: {b: {c: [3]}}}, {a: {b: {c: [3]}}}],
[{a: {b: {c: [3]}}}, customMethod]
[{a: {b: {c: [3]}}}],
[customMethod]
]);

var products = [
Expand All @@ -133,8 +132,8 @@ test('processStates_customMethods', function() {
[{'products': products}, customMethod],
{'products': products, numProducts: 3},
[
[{'products': products}, {'products': products}],
[{'products': products, numProducts: 3}, customMethod]
[{'products': products}],
[customMethod]
]);

var expectedProducts = [
Expand All @@ -152,8 +151,8 @@ test('processStates_customMethods', function() {
[{'products': products}, customMethod],
{'products': expectedProducts},
[
[{'products': products}, {'products': products}],
[{'products': expectedProducts}, customMethod]
[{'products': products}],
[customMethod]
]);

customMethod = function() {
Expand All @@ -168,16 +167,16 @@ test('processStates_customMethods', function() {
[{'products': products}, customMethod],
{'products': products, orderTotal: 60},
[
[{'products': products}, {'products': products}],
[{'products': products, orderTotal: 60}, customMethod]
[{'products': products}],
[customMethod]
]);

// Test the behavior of processing custom methods where the methods throw
// errors.
var errorFunction = function() {
throw 'Scary Error';
};
assertProcessStates([errorFunction], {} , [[{}, errorFunction]]);
assertProcessStates([errorFunction], {} , [[errorFunction]]);

errorFunction = function() {
this.set('a', 1);
Expand All @@ -187,7 +186,7 @@ test('processStates_customMethods', function() {
assertProcessStates(
[errorFunction],
{a: 1},
[[{a: 1}, errorFunction]]);
[[errorFunction]]);

errorFunction = function() {
this.set('a', 3);
Expand All @@ -198,8 +197,8 @@ test('processStates_customMethods', function() {
[{a: 1, b: 2}, errorFunction],
{a: 3, b: 2},
[
[{a: 1, b: 2}, {a: 1, b: 2}],
[{a: 3, b: 2}, errorFunction]
[{a: 1, b: 2}],
[errorFunction]
]);

// Custom methods throwing errors shouldn't affect any messages further down
Expand All @@ -212,7 +211,7 @@ test('processStates_customMethods', function() {
[errorFunction, {a: 2}],
{a: 2},
[
[{a: 1}, errorFunction],
[{a: 2}, {a: 2}]
[errorFunction],
[{a: 2}]
]);
});