|
| 1 | +import type { RenderingTestContext } from '@ember/test-helpers'; |
| 2 | + |
| 3 | +import { getService } from '@universal-ember/test-support'; |
| 4 | +import { module, test } from 'qunit'; |
| 5 | + |
| 6 | +import { |
| 7 | + PermissionsContextName, |
| 8 | + type Permissions, |
| 9 | + baseRealm, |
| 10 | +} from '@cardstack/runtime-common'; |
| 11 | +import type { Loader } from '@cardstack/runtime-common/loader'; |
| 12 | + |
| 13 | +import { |
| 14 | + provideConsumeContext, |
| 15 | + setupCardLogs, |
| 16 | + setupIntegrationTestRealm, |
| 17 | + setupLocalIndexing, |
| 18 | +} from '../../helpers'; |
| 19 | +import { |
| 20 | + setupBaseRealm, |
| 21 | + CardDef, |
| 22 | + Component, |
| 23 | + MarkdownField, |
| 24 | + contains, |
| 25 | + field, |
| 26 | +} from '../../helpers/base-realm'; |
| 27 | +import { setupMockMatrix } from '../../helpers/mock-matrix'; |
| 28 | +import { renderCard } from '../../helpers/render-component'; |
| 29 | +import { setupRenderingTest } from '../../helpers/setup'; |
| 30 | + |
| 31 | +let loader: Loader; |
| 32 | + |
| 33 | +module('Integration | MarkdownField', function (hooks) { |
| 34 | + setupRenderingTest(hooks); |
| 35 | + |
| 36 | + let mockMatrixUtils = setupMockMatrix(hooks); |
| 37 | + |
| 38 | + setupBaseRealm(hooks); |
| 39 | + |
| 40 | + hooks.beforeEach(function (this: RenderingTestContext) { |
| 41 | + let permissions: Permissions = { |
| 42 | + canWrite: true, |
| 43 | + canRead: true, |
| 44 | + }; |
| 45 | + provideConsumeContext(PermissionsContextName, permissions); |
| 46 | + loader = getService('loader-service').loader; |
| 47 | + }); |
| 48 | + setupLocalIndexing(hooks); |
| 49 | + |
| 50 | + setupCardLogs( |
| 51 | + hooks, |
| 52 | + async () => await loader.import(`${baseRealm.url}card-api`), |
| 53 | + ); |
| 54 | + |
| 55 | + test('embedded renders inline :card references as BFM elements for URL-form refs', async function (assert) { |
| 56 | + class TestCard extends CardDef { |
| 57 | + @field body = contains(MarkdownField); |
| 58 | + static embedded = class Embedded extends Component<typeof this> { |
| 59 | + <template><@fields.body /></template> |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + await setupIntegrationTestRealm({ |
| 64 | + mockMatrixUtils, |
| 65 | + contents: { |
| 66 | + 'test-card.gts': { TestCard }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + let card = new TestCard({ |
| 71 | + body: 'See :card[https://example.com/cards/1] for details.', |
| 72 | + }); |
| 73 | + let root = await renderCard(loader, card, 'embedded'); |
| 74 | + assert |
| 75 | + .dom(root.querySelector('[data-boxel-bfm-inline-ref]')) |
| 76 | + .exists('inline card reference is rendered as BFM element'); |
| 77 | + assert |
| 78 | + .dom(root.querySelector('[data-boxel-bfm-inline-ref]')) |
| 79 | + .hasAttribute('data-boxel-bfm-inline-ref', 'https://example.com/cards/1'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('embedded renders inline :card references for prefix-form refs without crashing', async function (assert) { |
| 83 | + // Regression test for the CardContextConsumer plumbing in |
| 84 | + // MarkdownField.embedded. Before VN was threaded through the field |
| 85 | + // default, prefix-form refs in MarkdownField content couldn't be |
| 86 | + // resolved at all (no VN reached the resolveUrl helper); with VN |
| 87 | + // required at the MarkdownTemplate boundary, this render would |
| 88 | + // throw if the consumer plumbing weren't in place. |
| 89 | + class TestCard extends CardDef { |
| 90 | + @field body = contains(MarkdownField); |
| 91 | + static embedded = class Embedded extends Component<typeof this> { |
| 92 | + <template><@fields.body /></template> |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + await setupIntegrationTestRealm({ |
| 97 | + mockMatrixUtils, |
| 98 | + contents: { |
| 99 | + 'test-card.gts': { TestCard }, |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + let card = new TestCard({ |
| 104 | + body: 'See :card[@cardstack/catalog/Card/foo] for details.', |
| 105 | + }); |
| 106 | + let root = await renderCard(loader, card, 'embedded'); |
| 107 | + assert |
| 108 | + .dom(root.querySelector('[data-boxel-bfm-inline-ref]')) |
| 109 | + .exists('prefix-form card reference renders as BFM element'); |
| 110 | + assert |
| 111 | + .dom(root.querySelector('[data-boxel-bfm-inline-ref]')) |
| 112 | + .hasAttribute( |
| 113 | + 'data-boxel-bfm-inline-ref', |
| 114 | + '@cardstack/catalog/Card/foo', |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + test('atom renders inline :card references as BFM elements', async function (assert) { |
| 119 | + class TestCard extends CardDef { |
| 120 | + @field body = contains(MarkdownField); |
| 121 | + static atom = class Atom extends Component<typeof this> { |
| 122 | + <template><@fields.body /></template> |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + await setupIntegrationTestRealm({ |
| 127 | + mockMatrixUtils, |
| 128 | + contents: { |
| 129 | + 'test-card.gts': { TestCard }, |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + let card = new TestCard({ |
| 134 | + body: 'See :card[https://example.com/cards/2] for details.', |
| 135 | + }); |
| 136 | + let root = await renderCard(loader, card, 'atom'); |
| 137 | + assert |
| 138 | + .dom(root.querySelector('[data-boxel-bfm-inline-ref]')) |
| 139 | + .exists('inline card reference is rendered in atom format'); |
| 140 | + assert |
| 141 | + .dom(root.querySelector('[data-boxel-bfm-inline-ref]')) |
| 142 | + .hasAttribute('data-boxel-bfm-inline-ref', 'https://example.com/cards/2'); |
| 143 | + }); |
| 144 | +}); |
0 commit comments