-
Notifications
You must be signed in to change notification settings - Fork 12
Get types to work inside of a realm test file locally #4181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tintinthong
wants to merge
4
commits into
main
Choose a base branch
from
getting-test-to-work-for-test-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| }, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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