forked from premieroctet/openchakra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds test, fix bug on delete component
- Loading branch information
Showing
6 changed files
with
201 additions
and
49 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
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,8 @@ | ||
export const generateId = () => { | ||
return `comp-${( | ||
Date.now().toString(36) + | ||
Math.random() | ||
.toString(36) | ||
.substr(2, 5) | ||
).toUpperCase()}` | ||
} |
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,127 @@ | ||
import { generateId } from './generateId' | ||
import { duplicateComponent, deleteComponent } from './recursive' | ||
|
||
jest.mock('./generateId') | ||
|
||
const mockGenerateId: jest.Mock<typeof generateId> = generateId as any | ||
|
||
describe('recursive functions', () => { | ||
let id = 1 | ||
let mockFn: typeof mockGenerateId | ||
|
||
beforeAll(() => { | ||
// @ts-ignore | ||
mockFn = mockGenerateId.mockImplementation(() => { | ||
return `comp-${id++}` | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
id = 1 | ||
}) | ||
|
||
afterAll(() => { | ||
mockFn.mockClear() | ||
}) | ||
|
||
it('should duplicate a Box component containing an Avatar', () => { | ||
const initialComponents: IComponents = {} | ||
|
||
const first = generateId() | ||
const second = generateId() | ||
|
||
initialComponents.root = { | ||
id: 'root', | ||
type: 'Box', | ||
parent: 'root', | ||
props: {}, | ||
children: [first], | ||
} | ||
|
||
initialComponents[first] = { | ||
id: first, | ||
type: 'Box', | ||
parent: 'root', | ||
props: {}, | ||
children: [second], | ||
} | ||
|
||
initialComponents[second] = { | ||
id: second, | ||
type: 'Avatar', | ||
parent: first, | ||
props: {}, | ||
children: [], | ||
} | ||
|
||
const { clonedComponents } = duplicateComponent( | ||
initialComponents[first], | ||
initialComponents, | ||
) | ||
|
||
expect(Object.keys(clonedComponents).length).toEqual(2) | ||
|
||
const finalTree = { | ||
...initialComponents, | ||
...clonedComponents, | ||
} | ||
|
||
const avatars = Object.keys(finalTree).filter( | ||
compKey => finalTree[compKey].type === 'Avatar', | ||
) | ||
const boxes = Object.keys(finalTree).filter( | ||
compKey => finalTree[compKey].type === 'Box', | ||
) | ||
|
||
expect(avatars.length).toEqual(2) | ||
expect(boxes.length).toEqual(3) | ||
}) | ||
|
||
it('should remove a Box containing an avatar and a badge', () => { | ||
const initialComponents: IComponents = {} | ||
|
||
const first = generateId() | ||
const second = generateId() | ||
const third = generateId() | ||
|
||
initialComponents.root = { | ||
id: 'root', | ||
type: 'Box', | ||
parent: 'root', | ||
props: {}, | ||
children: [first], | ||
} | ||
|
||
initialComponents[first] = { | ||
id: first, | ||
type: 'Box', | ||
parent: 'root', | ||
props: {}, | ||
children: [second], | ||
} | ||
|
||
initialComponents[second] = { | ||
id: second, | ||
type: 'Avatar', | ||
parent: first, | ||
props: {}, | ||
children: [third], | ||
} | ||
|
||
initialComponents[third] = { | ||
id: third, | ||
type: 'AvatarBadge', | ||
parent: second, | ||
props: {}, | ||
children: [], | ||
} | ||
|
||
const updatedComponents = deleteComponent( | ||
initialComponents[first], | ||
initialComponents, | ||
) | ||
|
||
expect(Object.keys(updatedComponents).length).toEqual(1) | ||
expect(Object.keys(updatedComponents)[0]).toEqual('root') | ||
}) | ||
}) |
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,58 @@ | ||
import omit from 'lodash/omit' | ||
import { generateId } from './generateId' | ||
|
||
export const duplicateComponent = ( | ||
componentToClone: IComponent, | ||
components: IComponents, | ||
) => { | ||
const clonedComponents: IComponents = {} | ||
|
||
const cloneComponent = (component: IComponent) => { | ||
const newid = generateId() | ||
const children = component.children.map(child => { | ||
return cloneComponent(components[child]) | ||
}) | ||
|
||
clonedComponents[newid] = { | ||
...component, | ||
id: newid, | ||
props: { ...component.props }, | ||
children, | ||
} | ||
|
||
children.forEach(child => { | ||
clonedComponents[child].parent = newid | ||
}) | ||
|
||
return newid | ||
} | ||
|
||
const newId = cloneComponent(componentToClone) | ||
|
||
return { | ||
newId, | ||
clonedComponents, | ||
} | ||
} | ||
|
||
export const deleteComponent = ( | ||
component: IComponent, | ||
components: IComponents, | ||
) => { | ||
let updatedComponents = { ...components } | ||
const deleteRecursive = ( | ||
children: IComponent['children'], | ||
id: IComponent['id'], | ||
) => { | ||
children.forEach(child => { | ||
updatedComponents[child] && | ||
deleteRecursive(updatedComponents[child].children, child) | ||
}) | ||
|
||
updatedComponents = omit(updatedComponents, id) | ||
} | ||
|
||
deleteRecursive(component.children, component.id) | ||
updatedComponents = omit(updatedComponents, component.id) | ||
return updatedComponents | ||
} |