Skip to content

Directive test example #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}