Skip to content

Commit

Permalink
Tests for transition.merge. #20
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 2, 2016
1 parent 302ec54 commit 557d7bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ transition
.transition(transition)
```

<a name="transition_merge" href="#transition_merge">#</a> <i>transition</i>.<b>merge</b>(<i>selection</i>)
<a name="transition_merge" href="#transition_merge">#</a> <i>transition</i>.<b>merge</b>(<i>other</i>)

Returns a new transition merging this transition with the specified *selection* (or *transition*). The returned transition has the same number of groups, the same parents, the same name and the same id as this transition. Any missing (null) elements in this transition are filled with the corresponding element, if present (not null), from the specified *selection*.
Returns a new transition merging this transition with the specified *other* transition, which must have the same id as this transition. The returned transition has the same number of groups, the same parents, the same name and the same id as this transition. Any missing (null) elements in this transition are filled with the corresponding element, if present (not null), from the *other* transition.

This method is equivalent to deriving the selection for this transition via [*transition*.selection](#transition_selection), merging with the specified selection via [*selection*.merge](https://github.com/d3/d3-selection#selection_merge), and then creating a new transition via [*selection*.transition](#selection_transition):
This method is equivalent to deriving the selection for this transition via [*transition*.selection](#transition_selection), merging with the selection likewise derived from the *other* transition via [*selection*.merge](https://github.com/d3/d3-selection#selection_merge), and then creating a new transition via [*selection*.transition](#selection_transition):

```js
transition
.selection()
.merge(selection)
.merge(other.selection())
.transition(transition)
```

Expand Down
5 changes: 3 additions & 2 deletions src/transition/merge.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Transition} from "./index";

export default function(selection) {
export default function(transition) {
if (transition._id !== this._id) throw new Error;

for (var groups0 = this._groups, groups1 = selection._groups, m0 = groups0.length, m1 = groups1.length, m = Math.min(m0, m1), merges = new Array(m0), j = 0; j < m; ++j) {
for (var groups0 = this._groups, groups1 = transition._groups, m0 = groups0.length, m1 = groups1.length, m = Math.min(m0, m1), merges = new Array(m0), j = 0; j < m; ++j) {
for (var group0 = groups0[j], group1 = groups1[j], n = group0.length, merge = merges[j] = new Array(n), node, i = 0; i < n; ++i) {
if (node = group0[i] || group1[i]) {
merge[i] = node;
Expand Down
29 changes: 29 additions & 0 deletions test/transition/merge-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var tape = require("tape"),
jsdom = require("jsdom"),
d3_selection = require("d3-selection"),
d3_transition = require("../../");

tape("transition.merge(other) merges elements from the specified other transition for null elements in this transition", function(test) {
var document = jsdom.jsdom("<h1 id='one'></h1><h1 id='two'></h1>"),
one = document.querySelector("#one"),
two = document.querySelector("#two"),
transition1 = d3_selection.selectAll([null, two]).transition(),
transition2 = d3_selection.selectAll([one, null]).transition(transition1),
transition3 = transition1.merge(transition2);
test.equal(transition3 instanceof d3_transition.transition, true);
test.deepEqual(transition3._groups, [[one, two]]);
test.equal(transition3._parents, transition1._parents);
test.equal(transition3._name, transition1._name);
test.equal(transition3._id, transition1._id);
test.end();
});

tape("transition.merge(other) throws an error if the other transition has a different id", function(test) {
var document = jsdom.jsdom("<h1 id='one'></h1><h1 id='two'></h1>"),
one = document.querySelector("#one"),
two = document.querySelector("#two"),
transition1 = d3_selection.selectAll([null, two]).transition(),
transition2 = d3_selection.selectAll([one, null]).transition();
test.throws(function() { transition1.merge(transition2); }, /Error/);
test.end();
});

0 comments on commit 557d7bb

Please sign in to comment.