@@ -12,3 +12,95 @@ export class SampleCommandCard extends CardDef {
1212 </template >
1313 };
1414}
15+
16+ // ── Tests (imports resolved via loader.shimModule in live-test.js) ────────────
17+ import { on } from ' @ember/modifier' ;
18+ import { service } from ' @ember/service' ;
19+ import { click , render } from ' @ember/test-helpers' ;
20+ import GlimmerComponent from ' @glimmer/component' ;
21+ import { module , test } from ' qunit' ;
22+
23+ import { getService } from ' @universal-ember/test-support' ;
24+ import { baseRealm } from ' @cardstack/runtime-common' ;
25+ import type StoreService from ' @cardstack/host/services/store' ;
26+ import {
27+ setupIntegrationTestRealm ,
28+ setupLocalIndexing ,
29+ setupOnSave ,
30+ setupCardLogs ,
31+ testRealmURL ,
32+ setupRealmCacheTeardown ,
33+ withCachedRealmSetup ,
34+ type TestContextWithSave ,
35+ } from ' @cardstack/host/tests/helpers' ;
36+ import { setupMockMatrix } from ' @cardstack/host/tests/helpers/mock-matrix' ;
37+ import { setupRenderingTest } from ' @cardstack/host/tests/helpers/setup' ;
38+ import type { TestRealmAdapter } from ' @cardstack/host/tests/helpers/adapter' ;
39+
40+ class CreateCardButton extends GlimmerComponent {
41+ @service declare store: StoreService ;
42+
43+ createCard = async () => {
44+ await this .store .add (new SampleCommandCard ({ title: ' Hello from live-test' }));
45+ };
46+
47+ <template >
48+ <button type =' button' {{on ' click' this . createCard}} >Create Card</button >
49+ </template >
50+ }
51+
52+ module (' Experiments | SampleCommandCard' , function (hooks ) {
53+ setupRenderingTest (hooks );
54+ setupLocalIndexing (hooks );
55+ setupOnSave (hooks );
56+ setupRealmCacheTeardown (hooks );
57+ setupCardLogs (hooks , async () =>
58+ (getService (' loader-service' ) as any ).loader .import (` ${baseRealm .url }card-api ` ),
59+ );
60+
61+ let mockMatrixUtils = setupMockMatrix (hooks , {
62+ loggedInAs: ' @testuser:localhost' ,
63+ activeRealms: [testRealmURL ],
64+ autostart: true ,
65+ });
66+
67+ let testRealmAdapter: TestRealmAdapter ;
68+
69+ hooks .beforeEach (async function () {
70+ ({ adapter: testRealmAdapter } = await withCachedRealmSetup (async () =>
71+ setupIntegrationTestRealm ({
72+ mockMatrixUtils ,
73+ contents: {
74+ ' sample-command-card.gts' : { SampleCommandCard },
75+ ' .realm.json' : ' { "name": "Sample Realm" }' ,
76+ },
77+ }),
78+ ));
79+ });
80+
81+ test (' clicking Create Card writes a new card to the realm' , async function (
82+ this : TestContextWithSave ,
83+ assert ,
84+ ) {
85+ assert .expect (3 );
86+
87+ let savedUrl: URL | undefined ;
88+ this .onSave ((url , doc ) => {
89+ savedUrl = url ;
90+ assert .strictEqual (
91+ (doc as any ).data .attributes .title ,
92+ ' Hello from live-test' ,
93+ ' saved doc has correct title' ,
94+ );
95+ });
96+
97+ await render (<template ><CreateCardButton /></template >);
98+ await click (' button' );
99+
100+ assert .ok (savedUrl , ' card was saved to realm' );
101+ let relativePath = ` ${savedUrl ! .href .substring (testRealmURL .length )}.json ` ;
102+ let file = await testRealmAdapter .openFile (relativePath );
103+ assert .ok (file , ' card JSON file exists in the realm adapter' );
104+ },
105+ );
106+ });
0 commit comments