Skip to content

Commit

Permalink
chore: Add directive test example (#120)
Browse files Browse the repository at this point in the history
* Add directive folder to test path ignore patterns to jest config

* Implement v-uppercase directive

* Implement Directive example component

* Implement directive test
  • Loading branch information
edufarre committed Feb 22, 2020
1 parent 322f3a9 commit cd4deb8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = merge(config, {
testPathIgnorePatterns: [
'<rootDir>/node_modules',
'<rootDir>/src/__tests__/components',
'<rootDir>/src/__tests__/directives',
],
})
15 changes: 15 additions & 0 deletions src/__tests__/components/Directive.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<div>
<p v-uppercase="text" />
</div>
</template>

<script>
export default {
data() {
return {
text: 'example text',
}
},
}
</script>
20 changes: 20 additions & 0 deletions src/__tests__/directive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {render} from '@testing-library/vue'
import '@testing-library/jest-dom/extend-expect'
import {uppercaseDirective} from './directives/uppercase-directive'
import Directive from './components/Directive'

// We are about to test an easy vue directive, that we have implemented,
// named v-uppercawse.
test('Component with a custom directive', () => {
// Do not forget to add the new custom directive to the render function as
// the third parameter.
const {queryByText} = render(Directive, {}, vue =>
vue.directive('uppercase', uppercaseDirective),
)

// Test that the text in lower case does not appear in the DOM
expect(queryByText('example text')).not.toBeInTheDocument()

// Test that the text in upper case does appear in the DOM thanks to the directive
expect(queryByText('EXAMPLE TEXT')).toBeInTheDocument()
})
6 changes: 6 additions & 0 deletions src/__tests__/directives/uppercase-directive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This function converts the received text passed to the
// v-uppercase directive used in the Directive.vue component
// to upper case and this is appended to the <p> element
export function uppercaseDirective(el, binding) {
el.innerHTML = binding.value.toUpperCase()
}

0 comments on commit cd4deb8

Please sign in to comment.