Skip to content

Commit

Permalink
chore(tests): demo example of Issue#48
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jun 29, 2021
1 parent 3d0e94f commit 562c067
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ class MyResource extends LifecycleResource<Named<{ bananas: number }>> {
}
```

These shorthands are 3 characters sharter than using the `named:` or
`positional: ` keys that would be required if not using these shorthands...


## Testing

If your resources are consumed by components, you'll want to continue to
Expand Down
30 changes: 29 additions & 1 deletion tests/unit/use-resource-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module('useResource', function () {
setupTest(hooks);

test('it works', async function (assert) {
class Doubler extends LifecycleResource<{ positional: [number] }> {
class Doubler extends LifecycleResource<Positional<[number]>> {
@tracked num = 0;

setup() {
Expand Down Expand Up @@ -49,6 +49,34 @@ module('useResource', function () {
assert.equal(foo.data.num, 8);
});

test('can take a typed array https://github.com/NullVoxPopuli/ember-resources/issues/48', async function (assert) {
class DoubleEverything extends LifecycleResource<Positional<number[]>> {
@tracked result: number[] = [];

setup() {
this.update();
}

update() {
this.result = this.args.positional.map((num) => num * 2);
}
}
class Test {
@tracked numbers = [0, 1, 2, 3];

data = useResource(this, DoubleEverything, () => [...this.numbers]);
}

let foo = new Test();

await settled();
assert.deepEqual(foo.data.result, [0, 2, 4, 6]);

foo.numbers = [2, 6, 2, 7];
await settled();
assert.deepEqual(foo.data.result, [4, 12, 4, 14]);
});

test('lifecycle', async function (assert) {
class Doubler extends LifecycleResource<{ positional: [number] }> {
@tracked num = 0;
Expand Down

0 comments on commit 562c067

Please sign in to comment.