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

[BUGFIX release-1-13] Ensure concat streams unsubscribe properly. #12256

Merged
merged 1 commit into from
Sep 1, 2015
Merged
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
2 changes: 1 addition & 1 deletion packages/ember-metal/lib/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function concat(array, separator) {
});

for (i = 0, l = array.length; i < l; i++) {
subscribe(array[i], stream.notify, stream);
stream.addDependency(array[i]);
}

// used by angle bracket components to detect an attribute was provided
Expand Down
63 changes: 63 additions & 0 deletions packages/ember-metal/tests/streams/concat_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Stream from 'ember-metal/streams/stream';
import {
concat,
read
} from 'ember-metal/streams/utils';


function hasSubscribers(stream) {
// this uses the private internal property `subscriberHead`
// for the purposes of ensuring that subscription is cleared
// after deactivation. Adding a util helper to the `Stream` code
// just for the test seems dubious, as does accessing the private
// property directly in the test.
return stream && !!stream.subscriberHead;
}

QUnit.module('Stream - concat');

QUnit.test('returns string if no streams were in the array', function(assert) {
let result = concat(['foo', 'bar', 'baz'], ' ');

assert.equal(result, 'foo bar baz');
});

QUnit.test('returns a stream if a stream is in the array', function(assert) {
let stream = new Stream(function() {
return 'bar';
});
let result = concat(['foo', stream, 'baz'], ' ');

assert.ok(result.isStream, 'a stream is returned');
assert.equal(read(result), 'foo bar baz');
});

QUnit.test('returns updated value upon input dirtied', function(assert) {
let value = 'bar';
let stream = new Stream(function() {
return value;
});
let result = concat(['foo', stream, 'baz'], ' ');
result.activate();

assert.equal(read(result), 'foo bar baz');

value = 'qux';
stream.notify();

assert.equal(read(result), 'foo qux baz');
});

QUnit.test('removes dependencies when unsubscribeDependencies is called', function(assert) {
let stream = new Stream(function() {
return 'bar';
});
let result = concat(['foo', stream, 'baz'], ' ');
result.activate();

assert.equal(hasSubscribers(stream), true, 'subscribers are present from the concat stream');

result.maybeDeactivate();

assert.equal(hasSubscribers(stream), false, 'subscribers are removed after concat stream is deactivated');
});