Skip to content

Commit

Permalink
test: add test for LinkPropController
Browse files Browse the repository at this point in the history
  • Loading branch information
fikrikarim committed Apr 4, 2024
1 parent 87f1daa commit f15b9e4
Show file tree
Hide file tree
Showing 8 changed files with 898 additions and 8 deletions.
5 changes: 5 additions & 0 deletions packages/prop-controllers/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"transform": {
"^.+\\.(t|j)sx?$": "@swc/jest"
}
}
6 changes: 5 additions & 1 deletion packages/prop-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
"types": "./dist/types/index.d.ts",
"scripts": {
"dev": "concurrently -k 'tsc --watch --preserveWatchOutput' 'tsup src/index.ts --watch'",
"build": "tsc && tsup src/index.ts"
"build": "tsc && tsup src/index.ts",
"test": "jest"
},
"dependencies": {
"ts-pattern": "^5.0.8"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@swc/jest": "^0.2.36",
"concurrently": "^8.2.2",
"jest": "^29.0.1",
"tsup": "^8.0.2"
}
}
1 change: 1 addition & 0 deletions packages/prop-controllers/src/link/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './link'
295 changes: 295 additions & 0 deletions packages/prop-controllers/src/link/link.test.ts
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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CopyContext,
Options,
Types,
} from "./prop-controllers"
} from "../prop-controllers"

type OpenPageLink = {
type: 'OPEN_PAGE'
Expand Down
8 changes: 6 additions & 2 deletions packages/prop-controllers/src/prop-controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ type Data =
| Data[]
| { [key: string]: Data }

type ElementData = { type: string; key: string; props: Record<string, Data> }
export type ElementData = {
type: string
key: string
props: Record<string, Data>
}

type ElementReference = { type: 'reference'; key: string; value: string }

type Element = ElementData | ElementReference

type ReplacementContext = {
export type ReplacementContext = {
elementHtmlIds: Set<string>
elementKeys: Map<string, string>
swatchIds: Map<string, string>
Expand Down
28 changes: 28 additions & 0 deletions packages/prop-controllers/src/utils/utils.ts
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>(),
}
}
Loading

0 comments on commit f15b9e4

Please sign in to comment.