-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for LinkPropController
- Loading branch information
1 parent
87f1daa
commit f15b9e4
Showing
8 changed files
with
898 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.(t|j)sx?$": "@swc/jest" | ||
} | ||
} |
This file contains 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 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 @@ | ||
export * from './link' |
This file contains 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,295 @@ | ||
import { describe, expect, test } from '@jest/globals' | ||
|
||
import { ControlDataTypeKey } from '../prop-controllers' | ||
import { | ||
LinkPropControllerData, | ||
LinkPropControllerDataV0, | ||
LinkPropControllerDataV1, | ||
LinkPropControllerDataV1Type, | ||
copyLinkPropControllerData, | ||
getLinkPropControllerPageIds, | ||
getLinkPropControllerValue, | ||
} from './link' | ||
import { createReplacementContext } from '../utils/utils' | ||
|
||
describe('LinkPropController', () => { | ||
describe('getLinkPropControllerValue', () => { | ||
test('returns value for LinkPropControllerDataV1Type', () => { | ||
// Arrange | ||
const data: LinkPropControllerDataV1 = { | ||
[ControlDataTypeKey]: LinkPropControllerDataV1Type, | ||
value: { | ||
type: 'OPEN_PAGE', | ||
payload: { | ||
pageId: 'UGFnZTpmNTdmMjQ2MS0wMGY3LTQzZWUtYmIwOS03ODdiNTUyYzUyYWQ=', | ||
openInNewTab: false, | ||
}, | ||
}, | ||
} | ||
|
||
// Act | ||
const result = getLinkPropControllerValue(data) | ||
|
||
// Assert | ||
expect(result).toEqual(data.value) | ||
}) | ||
|
||
test('returns value for LinkPropControllerDataV0 data', () => { | ||
// Arrange | ||
const data: LinkPropControllerDataV0 = { | ||
type: 'OPEN_PAGE', | ||
payload: { | ||
pageId: 'UGFnZTpmNTdmMjQ2MS0wMGY3LTQzZWUtYmIwOS03ODdiNTUyYzUyYWQ=', | ||
openInNewTab: false, | ||
}, | ||
} | ||
|
||
// Act | ||
const result = getLinkPropControllerValue(data) | ||
|
||
// Assert | ||
expect(result).toEqual(data) | ||
}) | ||
|
||
test('returns value for unknown data', () => { | ||
// Arrange | ||
const data = { | ||
test: 'unknown', | ||
} | ||
|
||
// Act | ||
// @ts-expect-error: invalid data | ||
const result = getLinkPropControllerValue(data) | ||
|
||
// Assert | ||
expect(result).toEqual(data) | ||
}) | ||
}) | ||
|
||
describe('getLinkPropControllerPageIds', () => { | ||
test('returns an empty array when linkData is null or undefined', () => { | ||
expect(getLinkPropControllerPageIds(null)).toEqual([]) | ||
expect(getLinkPropControllerPageIds(undefined)).toEqual([]) | ||
}) | ||
|
||
test('returns an empty array when link type is not OPEN_PAGE', () => { | ||
const linkData: LinkPropControllerData = { | ||
type: 'OPEN_URL', | ||
payload: { url: 'https://example.com', openInNewTab: false }, | ||
} | ||
|
||
expect(getLinkPropControllerPageIds(linkData)).toEqual([]) | ||
}) | ||
|
||
test('returns an empty array when pageId is null for OPEN_PAGE link type', () => { | ||
const linkData: LinkPropControllerData = { | ||
type: 'OPEN_PAGE', | ||
payload: { pageId: null, openInNewTab: false }, | ||
} | ||
|
||
expect(getLinkPropControllerPageIds(linkData)).toEqual([]) | ||
}) | ||
|
||
test('returns an array with the pageId for v0 OPEN_PAGE link type', () => { | ||
const pageId = 'UGFnZToxMjM0NTY3OA==' | ||
const linkData: LinkPropControllerData = { | ||
type: 'OPEN_PAGE', | ||
payload: { pageId, openInNewTab: false }, | ||
} | ||
|
||
expect(getLinkPropControllerPageIds(linkData)).toEqual([pageId]) | ||
}) | ||
|
||
test('returns an array with the pageId for v1 OPEN_PAGE link type', () => { | ||
const pageId = 'UGFnZToxMjM0NTY3OA==' | ||
const linkData: LinkPropControllerData = { | ||
[ControlDataTypeKey]: LinkPropControllerDataV1Type, | ||
value: { | ||
type: 'OPEN_PAGE', | ||
payload: { pageId, openInNewTab: false }, | ||
}, | ||
} | ||
|
||
expect(getLinkPropControllerPageIds(linkData)).toEqual([pageId]) | ||
}) | ||
}) | ||
|
||
describe('copyLinkPropControllerData', () => { | ||
test('returns undefined when data is undefined', () => { | ||
expect( | ||
copyLinkPropControllerData(undefined, { | ||
replacementContext: createReplacementContext({}), | ||
copyElement: (node) => node, | ||
}), | ||
).toEqual(undefined) | ||
}) | ||
|
||
test('replaces page id from replacement context for v1 OPEN_PAGE link', () => { | ||
// Arrange | ||
const pageId = 'UGFnZTpmNTdmMjQ2MS0wMGY3LTQzZWUtYmIwOS03ODdiNTUyYzUyYWQ=' | ||
const data: LinkPropControllerDataV1 = { | ||
[ControlDataTypeKey]: LinkPropControllerDataV1Type, | ||
value: { | ||
type: 'OPEN_PAGE', | ||
payload: { | ||
pageId, | ||
openInNewTab: false, | ||
}, | ||
}, | ||
} | ||
const expected = JSON.parse( | ||
JSON.stringify(data).replace(pageId, 'testing'), | ||
) | ||
|
||
const replacementContext = createReplacementContext({ | ||
pageIds: new Map([[pageId, 'testing']]), | ||
}) | ||
|
||
// Act | ||
const result = copyLinkPropControllerData(data, { | ||
replacementContext: replacementContext, | ||
copyElement: (node) => node, | ||
}) | ||
|
||
// Assert | ||
expect(result).toMatchObject(expected) | ||
}) | ||
|
||
test('replaces page id from replacement context for v0 OPEN_PAGE link', () => { | ||
// Arrange | ||
const pageId = 'UGFnZTpmNTdmMjQ2MS0wMGY3LTQzZWUtYmIwOS03ODdiNTUyYzUyYWQ=' | ||
const data: LinkPropControllerDataV0 = { | ||
type: 'OPEN_PAGE', | ||
payload: { | ||
pageId, | ||
openInNewTab: false, | ||
}, | ||
} | ||
const expected = JSON.parse( | ||
JSON.stringify(data).replace(pageId, 'testing'), | ||
) | ||
|
||
const replacementContext = createReplacementContext({ | ||
pageIds: new Map([[pageId, 'testing']]), | ||
}) | ||
|
||
// Act | ||
const result = copyLinkPropControllerData(data, { | ||
replacementContext: replacementContext, | ||
copyElement: (node) => node, | ||
}) | ||
|
||
// Assert | ||
expect(result).toMatchObject(expected) | ||
}) | ||
|
||
test('replaces element key from replacement context for v1 SCROLL_TO_ELEMENT link', () => { | ||
// Arrange | ||
const data: LinkPropControllerDataV1 = { | ||
[ControlDataTypeKey]: LinkPropControllerDataV1Type, | ||
value: { | ||
type: 'SCROLL_TO_ELEMENT', | ||
payload: { | ||
elementIdConfig: { | ||
elementKey: 'element-key', | ||
propName: 'prop-name', | ||
}, | ||
block: 'start', | ||
}, | ||
}, | ||
} | ||
const expected = JSON.parse( | ||
JSON.stringify(data).replace('element-key', 'new-element-key'), | ||
) | ||
|
||
const replacementContext = createReplacementContext({ | ||
elementKeys: new Map([['element-key', 'new-element-key']]), | ||
}) | ||
|
||
// Act | ||
const result = copyLinkPropControllerData(data, { | ||
replacementContext: replacementContext, | ||
copyElement: (node) => node, | ||
}) | ||
|
||
// Assert | ||
expect(result).toMatchObject(expected) | ||
}) | ||
|
||
test('replaces element key from replacement context for v0 SCROLL_TO_ELEMENT link', () => { | ||
// Arrange | ||
const data: LinkPropControllerDataV0 = { | ||
type: 'SCROLL_TO_ELEMENT', | ||
payload: { | ||
elementIdConfig: { | ||
elementKey: 'element-key', | ||
propName: 'prop-name', | ||
}, | ||
block: 'start', | ||
}, | ||
} | ||
const expected = JSON.parse( | ||
JSON.stringify(data).replace('element-key', 'new-element-key'), | ||
) | ||
|
||
const replacementContext = createReplacementContext({ | ||
elementKeys: new Map([['element-key', 'new-element-key']]), | ||
}) | ||
|
||
// Act | ||
const result = copyLinkPropControllerData(data, { | ||
replacementContext: replacementContext, | ||
copyElement: (node) => node, | ||
}) | ||
|
||
// Assert | ||
expect(result).toMatchObject(expected) | ||
}) | ||
}) | ||
|
||
test('handles v0 URL link type', () => { | ||
// Arrange | ||
const data: LinkPropControllerDataV0 = { | ||
type: 'OPEN_URL', | ||
payload: { | ||
url: 'https://example.com', | ||
openInNewTab: false, | ||
}, | ||
} | ||
|
||
const replacementContext = createReplacementContext() | ||
|
||
// Act | ||
const result = copyLinkPropControllerData(data, { | ||
replacementContext: replacementContext, | ||
copyElement: (node) => node, | ||
}) | ||
|
||
// Assert | ||
expect(result).toMatchObject(data) | ||
}) | ||
|
||
test('handles v1 URL link type', () => { | ||
// Arrange | ||
const data: LinkPropControllerDataV1 = { | ||
[ControlDataTypeKey]: LinkPropControllerDataV1Type, | ||
value: { | ||
type: 'OPEN_URL', | ||
payload: { | ||
url: 'https://example.com', | ||
openInNewTab: false, | ||
}, | ||
}, | ||
} | ||
const replacementContext = createReplacementContext() | ||
|
||
// Act | ||
const result = copyLinkPropControllerData(data, { | ||
replacementContext: replacementContext, | ||
copyElement: (node) => node, | ||
}) | ||
|
||
// Assert | ||
expect(result).toMatchObject(data) | ||
}) | ||
}) |
This file contains 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 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 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,28 @@ | ||
import { ElementData, ReplacementContext } from '../prop-controllers' | ||
|
||
export function createReplacementContext(options?: { | ||
elementHtmlIds?: Set<string> | ||
elementKeys?: Map<string, string> | ||
swatchIds?: Map<string, string> | ||
fileIds?: Map<string, string> | ||
typographyIds?: Map<string, string> | ||
tableIds?: Map<string, string> | ||
tableColumnIds?: Map<string, string> | ||
pageIds?: Map<string, string> | ||
globalElementIds?: Map<string, string> | ||
globalElementData?: Map<string, ElementData> | ||
}): ReplacementContext { | ||
return { | ||
elementHtmlIds: options?.elementHtmlIds ?? new Set<string>(), | ||
elementKeys: options?.elementKeys ?? new Map<string, string>(), | ||
swatchIds: options?.swatchIds ?? new Map<string, string>(), | ||
fileIds: options?.fileIds ?? new Map<string, string>(), | ||
typographyIds: options?.typographyIds ?? new Map<string, string>(), | ||
tableIds: options?.tableIds ?? new Map<string, string>(), | ||
tableColumnIds: options?.tableColumnIds ?? new Map<string, string>(), | ||
pageIds: options?.pageIds ?? new Map<string, string>(), | ||
globalElementIds: options?.globalElementIds ?? new Map<string, string>(), | ||
globalElementData: | ||
options?.globalElementData ?? new Map<string, ElementData>(), | ||
} | ||
} |
Oops, something went wrong.