Skip to content

Commit 66b273e

Browse files
committedFeb 14, 2025
add test
1 parent 78e79e9 commit 66b273e

File tree

1 file changed

+121
-3
lines changed

1 file changed

+121
-3
lines changed
 

‎packages/host/tests/acceptance/code-submode/playground-test.gts

+121-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { module, test, skip } from 'qunit';
55

66
import type { Realm } from '@cardstack/runtime-common';
77

8+
import type { Format } from 'https://cardstack.com/base/card-api';
9+
810
import {
911
percySnapshot,
1012
setupAcceptanceTestRealm,
@@ -339,9 +341,15 @@ export class BlogPost extends CardDef {
339341

340342
test('can populate playground preview with previous choices saved in local storage', async function (assert) {
341343
let selections = {
342-
[`${testRealmURL}author/Author`]: `${testRealmURL}Author/jane-doe.json`,
343-
[`${testRealmURL}blog-post/BlogPost`]: `${testRealmURL}BlogPost/remote-work.json`,
344-
[`${testRealmURL}blog-post/Category`]: `${testRealmURL}Category/city-design.json`,
344+
[`${testRealmURL}author/Author`]: {
345+
cardId: `${testRealmURL}Author/jane-doe`,
346+
},
347+
[`${testRealmURL}blog-post/BlogPost`]: {
348+
cardId: `${testRealmURL}BlogPost/remote-work`,
349+
},
350+
[`${testRealmURL}blog-post/Category`]: {
351+
cardId: `${testRealmURL}Category/city-design`,
352+
},
345353
};
346354
window.localStorage.setItem(
347355
'playground-selections',
@@ -719,4 +727,114 @@ export class BlogPost extends CardDef {
719727
});
720728
assert.dom('[data-test-post-title]').includesText('Hello Mad As a Hatter');
721729
});
730+
731+
test('can remember format choice via local storage', async function (assert) {
732+
const authorModuleId = `${testRealmURL}author/Author`;
733+
const categoryModuleId = `${testRealmURL}blog-post/Category`;
734+
const blogPostModuleId = `${testRealmURL}blog-post/BlogPost`;
735+
const authorId = `${testRealmURL}Author/jane-doe`;
736+
const categoryId1 = `${testRealmURL}Category/city-design`;
737+
const categoryId2 = `${testRealmURL}Category/future-tech`;
738+
const blogPostId1 = `${testRealmURL}BlogPost/mad-hatter`;
739+
const blogPostId2 = `${testRealmURL}BlogPost/remote-work`;
740+
741+
window.localStorage.setItem(
742+
'playground-selections',
743+
JSON.stringify({
744+
[`${authorModuleId}`]: {
745+
cardId: authorId,
746+
format: 'edit',
747+
},
748+
[`${categoryModuleId}`]: {
749+
cardId: categoryId1,
750+
format: 'embedded',
751+
},
752+
[`${blogPostModuleId}`]: {
753+
cardId: blogPostId1,
754+
},
755+
}),
756+
);
757+
const getSelection = (moduleId: string) => {
758+
let selections = window.localStorage.getItem('playground-selections');
759+
if (!selections) {
760+
throw new Error('No selections found in mock local storage');
761+
}
762+
return JSON.parse(selections)[moduleId];
763+
};
764+
const assertCorrectFormat = (
765+
cardId: string,
766+
format: Format,
767+
message?: string,
768+
) => {
769+
const dataAttr = `[data-test-playground-panel] [data-test-card="${cardId}"][data-test-card-format="${format}"]`;
770+
assert.dom(dataAttr).exists(message);
771+
};
772+
await visitOperatorMode({
773+
stacks: [],
774+
submode: 'code',
775+
codePath: `${testRealmURL}author.gts`,
776+
});
777+
assertCorrectFormat(authorId, 'edit');
778+
await click('[data-test-format-chooser-atom]'); // change selected format
779+
assertCorrectFormat(authorId, 'atom');
780+
assert.deepEqual(
781+
getSelection(authorModuleId),
782+
{
783+
cardId: authorId,
784+
format: 'atom',
785+
},
786+
'local storage is updated',
787+
);
788+
789+
await click('[data-test-file-browser-toggle]');
790+
await click('[data-test-file="blog-post.gts"]'); // change open file
791+
assertCorrectFormat(categoryId1, 'embedded');
792+
793+
await click('[data-test-instance-chooser]');
794+
await click('[data-option-index="1"]'); // change selected instance
795+
assertCorrectFormat(categoryId2, 'embedded');
796+
assert.deepEqual(
797+
getSelection(categoryModuleId),
798+
{
799+
cardId: categoryId2,
800+
format: 'embedded',
801+
},
802+
'local storage is updated',
803+
);
804+
805+
await click('[data-test-inspector-toggle]');
806+
await click('[data-test-boxel-selector-item-text="BlogPost"]'); // change selected module
807+
assertCorrectFormat(blogPostId1, 'isolated', 'default format is correct');
808+
await click('[data-test-format-chooser-fitted]'); // change selected format
809+
assert.deepEqual(getSelection(blogPostModuleId), {
810+
cardId: blogPostId1,
811+
format: 'fitted',
812+
});
813+
await click('[data-test-instance-chooser]');
814+
await click('[data-option-index="1"]'); // change selected instance
815+
assertCorrectFormat(blogPostId2, 'fitted');
816+
assert.deepEqual(getSelection(blogPostModuleId), {
817+
cardId: blogPostId2,
818+
format: 'fitted',
819+
});
820+
821+
let selections = window.localStorage.getItem('playground-selections');
822+
assert.strictEqual(
823+
selections,
824+
JSON.stringify({
825+
[`${authorModuleId}`]: {
826+
cardId: authorId,
827+
format: 'atom',
828+
},
829+
[`${categoryModuleId}`]: {
830+
cardId: categoryId2,
831+
format: 'embedded',
832+
},
833+
[`${blogPostModuleId}`]: {
834+
cardId: blogPostId2,
835+
format: 'fitted',
836+
},
837+
}),
838+
);
839+
});
722840
});

0 commit comments

Comments
 (0)