Skip to content

Commit 0a03008

Browse files
Merge pull request #602 from NullVoxPopuli/improve-cell-types
fix(cell): improve typings of the cell utility
2 parents 439aa83 + 5e3229d commit 0a03008

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

ember-resources/src/util/cell.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { tracked } from '@glimmer/tracking';
22
import { assert } from '@ember/debug';
33

4-
class Cell<T> {
5-
@tracked current: T | undefined;
4+
class Cell<Value = unknown> {
5+
@tracked declare current: Value;
66

77
constructor();
8-
constructor(initialValue: T);
9-
constructor(initialValue?: T) {
10-
this.current = initialValue;
8+
constructor(initialValue: Value);
9+
constructor(initialValue?: Value) {
10+
if (initialValue !== undefined) {
11+
this.current = initialValue;
12+
}
1113
}
1214

1315
toggle = () => {
@@ -54,8 +56,8 @@ class Cell<T> {
5456
* </template>
5557
* ```
5658
*/
57-
export function cell<T>(initialValue?: T): Cell<T> {
58-
if (initialValue) {
59+
export function cell<Value = unknown>(initialValue?: Value): Cell<Value> {
60+
if (initialValue !== undefined) {
5961
return new Cell(initialValue);
6062
}
6163

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { render, settled } from '@ember/test-helpers';
2-
import { hbs } from 'ember-cli-htmlbars';
32
import { module, test } from 'qunit';
43
import { setupRenderingTest } from 'ember-qunit';
54

@@ -9,18 +8,27 @@ module('Utils | cell | rendering', function (hooks) {
98
setupRenderingTest(hooks);
109

1110
test('it works', async function (assert) {
12-
let state = cell();
11+
let state = cell<string | undefined>();
1312

14-
this.setProperties({ state });
15-
16-
await render(hbs`{{this.state.current}}`);
13+
await render(<template>{{state.current}}</template>);
1714

1815
assert.dom().doesNotContainText('hello');
1916

2017
state.current = 'hello';
18+
await settled();
19+
assert.dom().hasText('hello');
20+
});
2121

22+
test('it works with an initial value', async function (assert) {
23+
let state = cell(0);
24+
25+
await render(<template>{{state.current}}</template>);
26+
27+
assert.dom().hasText('0');
28+
29+
state.current++;
2230
await settled();
2331

24-
assert.dom().hasText('hello');
32+
assert.dom().hasText('1');
2533
});
2634
});

0 commit comments

Comments
 (0)