Skip to content

Commit

Permalink
Completed applyDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Helvik committed Oct 20, 2019
1 parent 6e8aa4d commit 9ec2bf5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -33,6 +33,6 @@
]
},
"dependencies": {
"@adrianhelvik/fragdom": "^2.0.1"
"@adrianhelvik/fragdom": "^2.0.2"
}
}
8 changes: 7 additions & 1 deletion src/applyDiff.js
Expand Up @@ -3,6 +3,7 @@ import createNode from './createNode.js'

export default function applyDiff(container, diff) {
const pending = []
let async = true

if (container instanceof window.Element) {
container = document.wrap(container)
Expand All @@ -24,7 +25,12 @@ export default function applyDiff(container, diff) {
break
case 'remove node':
node.childNodes[index].remove()
node.reconcile() // XXX: Not needed when fragdom issue #1 is resolved
break
case 'add prop':
node.childNodes[index].setAttribute(action.key, action.value)
break
case 'remove prop':
node.childNodes[index].removeAttribute(action.key)
break
default:
throw Error(
Expand Down
67 changes: 47 additions & 20 deletions src/applyDiff.spec.js
Expand Up @@ -129,29 +129,30 @@ describe('on child elements', () => {
})

it('can remove multiple elements', () => {
applyDiff(
container,
createDiff(
null,
<div>
<h1>Hello world</h1>
<main>
<p>Foo bar</p>
</main>
</div>,
),
const diffA = createDiff(
null,
<container>
<first-child>Hello world</first-child>
<second-child>
<p>Foo bar</p>
</second-child>
</container>,
)
const diff = createDiff(
<div>
<h1>Hello world</h1>
<main>

const diffB = createDiff(
<container>
<first-child>Hello world</first-child>
<second-child>
<p>Foo bar</p>
</main>
</div>,
<div />,
</second-child>
</container>,
<container />,
)
applyDiff(container, diff)
expect(container.innerHTML).toBe('<div></div>')

applyDiff(container, diffA)
applyDiff(container, diffB)

expect(container.innerHTML).toBe('<container></container>')
})

it('can track nodes that should be updated after the initial mount', () => {
Expand Down Expand Up @@ -180,3 +181,29 @@ describe('on child elements', () => {
expect(pending[0].target.parentNode.tagName).toBe('DIV')
})
})

it('can set initial props', () => {
const diff = createDiff(null, <div class="foo" />)
applyDiff(container, diff)
expect(container.innerHTML).toBe('<div class="foo"></div>')
})

it('can add props', () => {
const diffA = createDiff(null, <div />)
const diffB = createDiff(<div />, <div class="foo" />)

applyDiff(container, diffA)
applyDiff(container, diffB)

expect(container.innerHTML).toBe('<div class="foo"></div>')
})

it('can remove props', () => {
const diffA = createDiff(null, <div class="foo" />)
const diffB = createDiff(<div class="foo" />, <div />)

applyDiff(container, diffA)
applyDiff(container, diffB)

expect(container.innerHTML).toBe('<div></div>')
})
2 changes: 1 addition & 1 deletion src/createNode.js
Expand Up @@ -39,7 +39,7 @@ export default function createNode(virtualNode, pending) {

for (const key of Object.keys(virtualNode.props || {})) {
if (key !== 'children') {
node.setAttribute(key, props[key])
node.setAttribute(key, virtualNode.props[key])
}
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@adrianhelvik/fragdom@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@adrianhelvik/fragdom/-/fragdom-2.0.0.tgz#43e8110cb41b3f23bbfcf3622e1af21ea1b84336"
integrity sha512-QRAeEqd5y66WM8X/yTrOe1vyPFI+Abu4q2XNWXIpXIDugLn1dwvdpkR1I1h3JwbfXymogdUL+OhWjpCHkGCjrg==
"@adrianhelvik/fragdom@^2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@adrianhelvik/fragdom/-/fragdom-2.0.2.tgz#334f0ef47bacdc765fb168778c5e0776897b98a9"
integrity sha512-tl2ghHzaJ5vlzkTvCo1xSO3yHOwDsrZBC9xuCaHIYA9APtjXuWirVfl7SV92Azyu0kwCc7rsg3T24uNSv05uwA==

"@babel/cli@^7.6.2":
version "7.6.2"
Expand Down

0 comments on commit 9ec2bf5

Please sign in to comment.