Skip to content

Commit d534f36

Browse files
committed
removes deviation from ecmascript array, fixes mobxjs#745
1 parent af4fb88 commit d534f36

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎src/types/observablearray.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,13 @@ export class ObservableArray<T> extends StubArray {
380380
}
381381

382382
toString(): string {
383-
return "[mobx.array] " + Array.prototype.toString.apply(this.$mobx.values, arguments);
383+
this.$mobx.atom.reportObserved();
384+
return Array.prototype.toString.apply(this.$mobx.values, arguments);
384385
}
385386

386387
toLocaleString(): string {
387-
return "[mobx.array] " + Array.prototype.toLocaleString.apply(this.$mobx.values, arguments);
388+
this.$mobx.atom.reportObserved();
389+
return Array.prototype.toLocaleString.apply(this.$mobx.values, arguments);
388390
}
389391
}
390392

‎test/array.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,43 @@ test('is array', function(t) {
273273
t.end();
274274
})
275275

276+
test('stringifies same as ecma array', function(t) {
277+
const x = mobx.observable([]);
278+
t.equal(x instanceof Array, true);
279+
280+
// would be cool if these two would return true...
281+
t.equal(x.toString(), "");
282+
t.equal(x.toLocaleString(), "");
283+
x.push(1, 2)
284+
t.equal(x.toString(), "1,2");
285+
t.equal(x.toLocaleString(), "1,2");
286+
t.end();
287+
})
288+
289+
test("observes when stringified", function (t) {
290+
const x = mobx.observable([]);
291+
let c = 0;
292+
mobx.autorun(function() {
293+
x.toString();
294+
c++;
295+
});
296+
x.push(1);
297+
t.equal(c, 2);
298+
t.end();
299+
})
300+
301+
test("observes when stringified to locale", function (t) {
302+
const x = mobx.observable([]);
303+
let c = 0;
304+
mobx.autorun(function() {
305+
x.toLocaleString();
306+
c++;
307+
});
308+
x.push(1);
309+
t.equal(c, 2);
310+
t.end();
311+
})
312+
276313
test('peek', function(t) {
277314
var x = mobx.observable([1, 2, 3]);
278315
t.deepEqual(x.peek(), [1, 2, 3]);

0 commit comments

Comments
 (0)