|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { render } from '@ember/test-helpers'; |
| 3 | +import { module, test } from 'qunit'; |
| 4 | +import { setupRenderingTest } from 'ember-qunit'; |
| 5 | + |
| 6 | +import { cell, resource, use } from 'ember-resources'; |
| 7 | + |
| 8 | +import type { Reactive} from 'ember-resources'; |
| 9 | + |
| 10 | +type Cell<T> = ReturnType<typeof cell<T>>; |
| 11 | + |
| 12 | + export type ClockNakedSignature = { |
| 13 | + percentage: Cell<number>; |
| 14 | + counter: Cell<number>; |
| 15 | +}; |
| 16 | + |
| 17 | +export type ClockSignature = Reactive<ClockNakedSignature>; |
| 18 | + |
| 19 | +interface Signature { |
| 20 | + Args: {}; |
| 21 | + Blocks: { |
| 22 | + default: [ClockNakedSignature]; |
| 23 | + }; |
| 24 | + Element: HTMLDivElement; |
| 25 | +} |
| 26 | + |
| 27 | +const Clock = resource(() => { |
| 28 | + const counter = cell(0); |
| 29 | + const percentage = cell(0); |
| 30 | + |
| 31 | + return { percentage, counter }; |
| 32 | +}); |
| 33 | + |
| 34 | + |
| 35 | +// use (the function) exposes a .current property, like a Cell |
| 36 | +class Refresher extends Component<Signature> { |
| 37 | + clock = use(this, Clock); |
| 38 | + |
| 39 | + <template>{{yield this.clock.current}}</template> |
| 40 | +} |
| 41 | + |
| 42 | +// with use (the decorator) the .current access is absorbed in an underlying getter |
| 43 | +class Refresher2 extends Component<Signature> { |
| 44 | + @use clock = Clock; |
| 45 | + |
| 46 | + <template>{{yield this.clock}}</template> |
| 47 | +} |
| 48 | + |
| 49 | +const keys = (o: Record<string, unknown>) => Object.keys(o).join(','); |
| 50 | + |
| 51 | +module('issues/1128', function(hooks) { |
| 52 | + setupRenderingTest(hooks); |
| 53 | + |
| 54 | + test('it works', async function (assert) { |
| 55 | + await render(<template> |
| 56 | + <Refresher as |r|> |
| 57 | + <output id="one-keys">{{keys r}}</output> |
| 58 | + <output id="one">{{r.percentage.current}}</output> |
| 59 | + </Refresher> |
| 60 | + <Refresher2 as |r|> |
| 61 | + <output id="two-keys">{{keys r}}</output> |
| 62 | + <output id="two">{{r.percentage.current}}</output> |
| 63 | + </Refresher2> |
| 64 | + </template>); |
| 65 | + |
| 66 | + assert.dom('#one-keys').hasText('percentage,counter'); |
| 67 | + assert.dom('#two-keys').hasText('percentage,counter'); |
| 68 | + assert.dom('#one').hasText('0'); |
| 69 | + assert.dom('#two').hasText('0'); |
| 70 | + }); |
| 71 | + |
| 72 | +}); |
0 commit comments