Skip to content

Commit 52a929c

Browse files
committed
accessing property inside of getter (but not template) triggers an unnecessary re-render
1 parent ed0c10d commit 52a929c

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

packages/host/tests/integration/components/card-api-test.gts

+104-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { BoxelInput } from '@cardstack/boxel-ui/components';
1414
import { Loader, CardContextName, type Query } from '@cardstack/runtime-common';
1515

1616
import { getSearch } from '@cardstack/host/resources/search';
17+
import { getCard } from '@cardstack/host/resources/card-resource';
1718
import LoaderService from '@cardstack/host/services/loader-service';
1819

1920
import {
@@ -161,7 +162,6 @@ module('Integration | card api (Usage of publicAPI actions)', function (hooks) {
161162
},
162163
});
163164
});
164-
165165
test(`changing query updates search resource`, async function (assert) {
166166
class Isolated extends Component<typeof AuthorSearch> {
167167
@tracked name = 'Jane';
@@ -254,6 +254,109 @@ module('Integration | card api (Usage of publicAPI actions)', function (hooks) {
254254
.exists({ count: 2 });
255255
});
256256
});
257+
258+
module('getCard', function (_) {
259+
hooks.beforeEach(async function (this: RenderingTestContext) {
260+
class StringFieldWithValue extends StringField {
261+
static displayName = 'StringFieldWithValue';
262+
263+
static edit = class Edit extends Component<
264+
typeof StringFieldWithValue
265+
> {
266+
<template>
267+
{{@model}}
268+
<BoxelInput @value={{@model}} @onInput={{@set}} />
269+
</template>
270+
};
271+
}
272+
273+
class SimpleCard extends CardDef {
274+
static displayName = 'SimpleCard';
275+
@field name = contains(StringFieldWithValue);
276+
277+
static edit = class Edit extends Component<typeof this> {
278+
<template>
279+
<@fields.name />
280+
</template>
281+
};
282+
}
283+
await setupIntegrationTestRealm({
284+
loader,
285+
mockMatrixUtils,
286+
contents: {
287+
'simple-card.gts': { SimpleCard },
288+
'simple-card.json': {
289+
data: {
290+
type: 'card',
291+
attributes: {
292+
name: 'Jane Doe',
293+
},
294+
meta: {
295+
adoptsFrom: {
296+
module: `${testRealmURL}simple-card`,
297+
name: 'SimpleCard',
298+
},
299+
},
300+
},
301+
},
302+
},
303+
});
304+
});
305+
306+
test('accessing property of card in getter should not cause re-render of the component', async function (assert) {
307+
class SampleUsageWithGetCard extends CardDef {
308+
static displayName = 'SampleUsageWithGetCard';
309+
@field name = contains(StringField);
310+
@field title = contains(StringField, {
311+
computeVia: function (this: SampleUsageWithGetCard) {
312+
return this.name;
313+
},
314+
});
315+
316+
static edit = class Edit extends Component<
317+
typeof SampleUsageWithGetCard
318+
> {
319+
get id() {
320+
return `${testRealmURL}simple-card`;
321+
}
322+
323+
private resource = getCard(this, () => this.id);
324+
325+
get card() {
326+
// console.log(this.resource.card.name); //<- this will cause a re-render
327+
return this.resource.card;
328+
}
329+
330+
<template>
331+
{{#if this.card}}
332+
{{#let (getComponent this.card) as |CardComponent|}}
333+
<CardComponent />
334+
{{/let}}
335+
{{else}}
336+
<div>
337+
Loading...
338+
</div>
339+
{{/if}}
340+
</template>
341+
};
342+
}
343+
let card = new SampleUsageWithGetCard();
344+
await renderComponent(
345+
class TestDriver extends GlimmerComponent {
346+
<template>
347+
<CardContextProvider>
348+
<CardContextConsumer>
349+
{{#let (getComponent card) as |Card|}}
350+
<Card @format='edit' />
351+
{{/let}}
352+
</CardContextConsumer>
353+
</CardContextProvider>
354+
</template>
355+
},
356+
);
357+
assert.ok(true);
358+
});
359+
});
257360
});
258361

259362
function getComponent(cardOrField: any) {

0 commit comments

Comments
 (0)