Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/catalog-realm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"@cardstack/boxel-ui": "workspace:*",
"@cardstack/local-types": "workspace:*",
"@cardstack/runtime-common": "workspace:*",
"@ember/test-helpers": "catalog:",
"@types/lodash": "catalog:",
"@types/qunit": "catalog:",
"@universal-ember/test-support": "catalog:",
"@types/uuid": "catalog:",
"chess.js": "catalog:",
"concurrently": "catalog:",
Expand Down
1 change: 1 addition & 0 deletions packages/catalog-realm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"types": ["@cardstack/local-types"]
},
"include": ["**/*.ts", "**/*.gts", "../local-types/test-support.d.ts"],
"glint": {
"environment": ["ember-loose", "ember-template-imports"]
}
Expand Down
3 changes: 3 additions & 0 deletions packages/experiments-realm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"@cardstack/boxel-ui": "workspace:*",
"@cardstack/local-types": "workspace:*",
"@cardstack/runtime-common": "workspace:*",
"@ember/test-helpers": "catalog:",
"@types/qunit": "catalog:",
"@universal-ember/test-support": "catalog:",
"@cardstack/view-transitions": "catalog:",
"@types/lodash": "catalog:",
"ember-animated": "catalog:",
Expand Down
106 changes: 106 additions & 0 deletions packages/experiments-realm/sample-command-card.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { CardDef, field, contains, Component } from 'https://cardstack.com/base/card-api';
import StringField from 'https://cardstack.com/base/string';

export class SampleCommandCard extends CardDef {
static displayName = 'Sample Command Card';
@field title = contains(StringField);

static isolated = class Isolated extends Component<typeof SampleCommandCard> {
<template>
<h1><@fields.title /></h1>
<button type='button'>Create Card</button>
</template>
};
}

// ── Tests (imports resolved via loader.shimModule in live-test.js) ────────────
import { on } from '@ember/modifier';
import { service } from '@ember/service';
import { click, render } from '@ember/test-helpers';
import GlimmerComponent from '@glimmer/component';
import { module, test } from 'qunit';

import { getService } from '@universal-ember/test-support';
import { baseRealm } from '@cardstack/runtime-common';
import type StoreService from '@cardstack/host/services/store';
import {
setupIntegrationTestRealm,
setupLocalIndexing,
setupOnSave,
setupCardLogs,
testRealmURL,
setupRealmCacheTeardown,
withCachedRealmSetup,
type TestContextWithSave,
} from '@cardstack/host/tests/helpers';
import { setupMockMatrix } from '@cardstack/host/tests/helpers/mock-matrix';
import { setupRenderingTest } from '@cardstack/host/tests/helpers/setup';
import type { TestRealmAdapter } from '@cardstack/host/tests/helpers/adapter';

class CreateCardButton extends GlimmerComponent {
@service declare store: StoreService;

createCard = async () => {
await this.store.add(new SampleCommandCard({ title: 'Hello from live-test' }));
};

<template>
<button type='button' {{on 'click' this.createCard}}>Create Card</button>
</template>
}

module('Experiments | SampleCommandCard', function (hooks) {
setupRenderingTest(hooks);
setupLocalIndexing(hooks);
setupOnSave(hooks);
setupRealmCacheTeardown(hooks);
setupCardLogs(hooks, async () =>
(getService('loader-service') as any).loader.import(`${baseRealm.url}card-api`),
);

let mockMatrixUtils = setupMockMatrix(hooks, {
loggedInAs: '@testuser:localhost',
activeRealms: [testRealmURL],
autostart: true,
});

let testRealmAdapter: TestRealmAdapter;

hooks.beforeEach(async function () {
({ adapter: testRealmAdapter } = await withCachedRealmSetup(async () =>
setupIntegrationTestRealm({
mockMatrixUtils,
contents: {
'sample-command-card.gts': { SampleCommandCard },
'.realm.json': '{ "name": "Sample Realm" }',
},
}),
));
});

test('clicking Create Card writes a new card to the realm', async function (
this: TestContextWithSave,
assert,
) {
assert.expect(3);

let savedUrl: URL | undefined;
this.onSave((url, doc) => {
savedUrl = url;
assert.strictEqual(
(doc as any).data.attributes.title,
'Hello from live-test',
'saved doc has correct title',
);
});

await render(<template><CreateCardButton /></template>);
await click('button');

assert.ok(savedUrl, 'card was saved to realm');
let relativePath = `${savedUrl!.href.substring(testRealmURL.length)}.json`;
let file = await testRealmAdapter.openFile(relativePath);
assert.ok(file, 'card JSON file exists in the realm adapter');
},
);
});
1 change: 1 addition & 0 deletions packages/experiments-realm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"types": ["@cardstack/local-types"]
},
"include": ["**/*.ts", "**/*.gts", "../local-types/test-support.d.ts"],
"glint": {
"environment": ["ember-loose", "ember-template-imports"]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@cardstack/view-transitions": "catalog:",
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.1.1",
"@ember/test-helpers": "^3.3.1",
"@ember/test-helpers": "catalog:",
"@ember/test-waiters": "catalog:",
"@embroider/compat": "^3.5.5",
"@embroider/core": "^3.4.15",
Expand Down
66 changes: 66 additions & 0 deletions packages/local-types/test-support.d.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just as a note, these types can end up diverging from the actual implementations in host. consider using an interface/type that that lives in runtime common or some other exportable package that both these types and the ones in host implement so that we can keep prevent diverging types

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Opt-in type declarations for colocated realm test files.
// Add this path to your realm package's tsconfig "include" array:
// "../local-types/test-support.d.ts"
//
// Also add to your realm package's devDependencies:
// "@ember/test-helpers": "catalog:"
// "@types/qunit": "catalog:"
// "@universal-ember/test-support": "catalog:"
//
// The declarations below cover @cardstack/host internal modules which have no
// separate npm package. All other test imports resolve via node_modules.

// ── @cardstack/host services ─────────────────────────────────────────────────
declare module '@cardstack/host/services/store' {
export default class StoreService {
add(instance: unknown): Promise<unknown>;
peek(id: string): unknown;
}
}

// ── @cardstack/host test helpers ─────────────────────────────────────────────
declare module '@cardstack/host/tests/helpers/adapter' {
export interface TestRealmAdapter {
openFile(path: string): Promise<{ content: string | Uint8Array } | undefined>;
}
}

declare module '@cardstack/host/tests/helpers' {
import type { TestRealmAdapter } from '@cardstack/host/tests/helpers/adapter';

export interface SetupRealmResult {
adapter: TestRealmAdapter;
}

export function setupIntegrationTestRealm(opts: {
mockMatrixUtils: unknown;
contents: Record<string, unknown>;
}): Promise<SetupRealmResult>;

export function setupLocalIndexing(hooks: unknown): void;
export function setupOnSave(hooks: unknown): void;
export function setupCardLogs(hooks: unknown, fn: () => Promise<unknown>): void;
export function setupRealmCacheTeardown(hooks: unknown): void;
export function withCachedRealmSetup<T>(fn: () => Promise<T>): Promise<T>;

export const testRealmURL: string;

export interface TestContextWithSave {
onSave(cb: (url: URL, doc: unknown) => void): void;
}
}

declare module '@cardstack/host/tests/helpers/mock-matrix' {
export function setupMockMatrix(
hooks: unknown,
opts: {
loggedInAs: string;
activeRealms: string[];
autostart: boolean;
},
): unknown;
}

declare module '@cardstack/host/tests/helpers/setup' {
export function setupRenderingTest(hooks: unknown): void;
}
39 changes: 30 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ catalog:
"@cardstack/requirejs-monaco-ember-polyfill": ^0.0.1
"@cardstack/view-transitions": ^0.2.0
"@ember/string": ^4.0.1
"@ember/test-helpers": ^3.3.1
"@ember/test-waiters": ^4.1.1
"@eslint/eslintrc": ^2.1.4
"@eslint/js": ^8.57.1
Expand Down
Loading