Skip to content

Commit

Permalink
Added test cases and installed upgraded fragdom
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Helvik committed Nov 3, 2019
1 parent 74990b1 commit 77e8693
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@adrianhelvik/vdom",
"version": "1.3.6",
"version": "1.3.7",
"main": "dist/index.js",
"module": "src/index.js",
"author": "Adrian Helvik <adrian.helvik@specifique.no>",
Expand Down
11 changes: 6 additions & 5 deletions src/applyDiff.js
Expand Up @@ -22,13 +22,14 @@ export default function applyDiff(container, diff, options = {}) {
const node = lookup(container, path)

switch (action.type) {
case 'replace node':
case 'replace node': {
unmountComponentAtNode(node.childNodes[index])
node.replaceChild(
createNode(action.node, pendingComponents),
node.childNodes[index],
)
const newChild = createNode(action.node, pendingComponents)
const oldChild = node.childNodes[index]

node.replaceChild(newChild, oldChild)
break
}
case 'insert node':
node.appendChild(createNode(action.node, pendingComponents))
break
Expand Down
28 changes: 28 additions & 0 deletions src/applyDiff.spec.js
Expand Up @@ -7,6 +7,7 @@ import applyDiff from './applyDiff.js'
jest.useFakeTimers()

let container
let fragContainer
let prev = null
let render = template => {
applyDiff(container, createDiff(prev, template))
Expand All @@ -15,6 +16,7 @@ let render = template => {

beforeEach(() => {
container = window.document.createElement('div')
fragContainer = fragdom.wrap(container)
})

afterEach(() => {
Expand All @@ -35,6 +37,32 @@ describe('directly', () => {
expect(container.innerHTML).toBe('<span></span>')
})

it('can replace a text node inside an element in a fragment', () => {
applyDiff(container, createDiff(null, [<div>Hi "{'a'}"</div>]))
let d
applyDiff(
container,
(d = createDiff(
<>
<div>Hi "{'a'}"</div>
</>,
<>
<div>Hi "{'b'}"</div>
</>,
)),
)
expect(
fragContainer.childNodes[0].childNodes[0].childNodes[1].textContent,
).toBe('b')

try {
expect(container.innerHTML).toBe('<div>Hi "b"</div>')
} catch (e) {
e.message = `[caused by fragdom] ${e.message}`
throw e
}
})

it('can remove an element', () => {
applyDiff(container, createDiff(null, <div />))
applyDiff(container, createDiff(<div />, null))
Expand Down
4 changes: 3 additions & 1 deletion src/bugfixes.spec.js
Expand Up @@ -3,6 +3,8 @@ import createElement from './createElement'
import createDiff from './createDiff'
import applyDiff from './applyDiff'

const { Fragment } = createElement

const prevTemplates = new WeakMap()
const mount = (curr, target) => {
let prev = prevTemplates.get(target)
Expand All @@ -14,7 +16,7 @@ const mount = (curr, target) => {
applyDiff(target, diff)
}

test('repro', () => {
test('repro 1', () => {
const root = document.createElement('div')

mount([<div class="todoName">By "{''}"</div>], root)
Expand Down

0 comments on commit 77e8693

Please sign in to comment.