diff --git a/__test__/entry-editable.test.ts b/__test__/entry-editable.test.ts index dc6fdc7..2b3f983 100644 --- a/__test__/entry-editable.test.ts +++ b/__test__/entry-editable.test.ts @@ -372,6 +372,151 @@ describe('Entry editable test', () => { done() }) + + it('Reference fields should not inherit parent variants when they have no applied_variants', done => { + const entryWithReferenceAndVariants = { + "title": "home", + "url": "/data/all_test/first", + "single_line": "ssd", + "tags": ["hi"], + "locale": "en-us", + "uid": "blt827e0ad3608248be", + "created_by": "bltf0d59057590e9b09", + "updated_by": "bltf0d59057590e9b09", + "created_at": "2025-08-25T09:43:49.935Z", + "updated_at": "2025-10-09T11:45:19.967Z", + "ACL": [] as any[], + "_version": 40, + "_in_progress": false, + "json_rte": "

hisdassf

", + "select": "1", + "group": { + "single_line": "" + }, + "non_single_line_textbox": "", + "reference": [ + { + "title": "base variant", + "single_line": "bases", + "tags": [] as any[], + "locale": "en-us", + "uid": "blt07a6c7258ddba844", + "created_by": "bltf0d59057590e9b09", + "updated_by": "bltf0d59057590e9b09", + "created_at": "2025-10-01T03:10:10.701Z", + "updated_at": "2025-10-09T11:44:44.981Z", + "_content_type_uid": "all_test_3", + "ACL": [] as any[], + "_version": 3, + "_in_progress": false, + "multi_line_reference": "hii\n" + // Note: This reference object has NO _applied_variants + } + ], + "taxonomies": [] as any[], + "multi_line": "woek", + "_applied_variants": { + "single_line": "csfff653e89df54e8c", + "tags": "csfff653e89df54e8c", + "multi_line": "csfff653e89df54e8c" + } + } + + addTags(entryWithReferenceAndVariants, 'all_test', false) + + // Parent entry fields with variants should get v2 prefix + expect((entryWithReferenceAndVariants as any)['$']['single_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.single_line') + expect((entryWithReferenceAndVariants as any)['$']['tags']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.tags') + expect((entryWithReferenceAndVariants as any)['$']['multi_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.multi_line') + + // Reference fields should NOT get v2 prefix since they have no _applied_variants + expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['title']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.title') + expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['single_line']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.single_line') + expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['tags']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.tags') + expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['multi_line_reference']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.multi_line_reference') + + done() + }) + + it('Reference fields with their own applied_variants should use their variants', done => { + const entryWithReferenceHavingVariants = { + "title": "home", + "locale": "en-us", + "uid": "blt827e0ad3608248be", + "ACL": [] as any[], + "_version": 40, + "_in_progress": false, + "single_line": "parent field", + "reference": [ + { + "title": "base variant", + "single_line": "reference field", + "locale": "en-us", + "uid": "blt07a6c7258ddba844", + "_content_type_uid": "all_test_3", + "ACL": [] as any[], + "_version": 3, + "_in_progress": false, + "_applied_variants": { + "single_line": "ref_variant_123" + } + } + ], + "_applied_variants": { + "single_line": "parent_variant_456" + } + } + + addTags(entryWithReferenceHavingVariants, 'all_test', false) + + // Parent entry field should get parent variant + expect((entryWithReferenceHavingVariants as any)['$']['single_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_parent_variant_456.en-us.single_line') + + // Reference field should get its own variant, not parent variant + expect((entryWithReferenceHavingVariants as any)['reference'][0]['$']['title']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.title') + expect((entryWithReferenceHavingVariants as any)['reference'][0]['$']['single_line']).toEqual('data-cslp=v2:all_test_3.blt07a6c7258ddba844_ref_variant_123.en-us.single_line') + + done() + }) + + it('Reference fields should work correctly with tagsAsObject=true', done => { + const entryWithReferenceAndVariants = { + "title": "home", + "single_line": "ssd", + "locale": "en-us", + "uid": "blt827e0ad3608248be", + "ACL": [] as any[], + "_version": 40, + "_in_progress": false, + "reference": [ + { + "title": "base variant", + "single_line": "bases", + "locale": "en-us", + "uid": "blt07a6c7258ddba844", + "_content_type_uid": "all_test_3", + "ACL": [] as any[], + "_version": 3, + "_in_progress": false + // No _applied_variants + } + ], + "_applied_variants": { + "single_line": "csfff653e89df54e8c" + } + } + + addTags(entryWithReferenceAndVariants, 'all_test', true) + + // Parent entry field with variant should get v2 prefix as object + expect((entryWithReferenceAndVariants as any)['$']['single_line']).toEqual({'data-cslp': 'v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.single_line'}) + + // Reference fields should NOT get v2 prefix as objects + expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['title']).toEqual({'data-cslp': 'all_test_3.blt07a6c7258ddba844.en-us.title'}) + expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['single_line']).toEqual({'data-cslp': 'all_test_3.blt07a6c7258ddba844.en-us.single_line'}) + + done() + }) }) }) \ No newline at end of file diff --git a/src/entry-editable.ts b/src/entry-editable.ts index 06f833e..6492a08 100644 --- a/src/entry-editable.ts +++ b/src/entry-editable.ts @@ -62,7 +62,7 @@ function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: * }] * } */ - const newAppliedVariants = obj._applied_variants || obj?.system?.applied_variants || _applied_variants; + const newAppliedVariants = obj._applied_variants || obj?.system?.applied_variants || null //check for _applied_variants in the reference object only return null if not present , do not check in the parent object; const newShouldApplyVariant = !!newAppliedVariants value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale, { _applied_variants: newAppliedVariants, shouldApplyVariant: newShouldApplyVariant, metaKey: "" }) } else if (typeof obj === "object") {