Skip to content

Added support for CSS modules in hasClass #106

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 5 commits into from
Oct 17, 2017
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
11 changes: 9 additions & 2 deletions src/wrappers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,18 @@ export default class Wrapper implements BaseWrapper {
* Asserts wrapper has a class name
*/
hasClass (className: string) {
if (typeof className !== 'string') {
let targetClass = className

if (typeof targetClass !== 'string') {
throwError('wrapper.hasClass() must be passed a string')
}

return !!(this.element && this.element.classList.contains(className))
// if $style is available and has a matching target, use that instead.
if (this.vm && this.vm.$style && this.vm.$style[targetClass]) {
targetClass = this.vm.$style[targetClass]
}

return !!(this.element && this.element.classList.contains(targetClass))
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/resources/components/component-with-css-modules.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<div :class="$style['color-red']"></div>
</template>

<style module>
.color-red {
color: red;
}
</style>

<script>
export default{
name: 'component-with-css-modules'
}
</script>
7 changes: 7 additions & 0 deletions test/unit/specs/mount/Wrapper/hasClass.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ComponentWithCssModules from '~resources/components/component-with-css-modules.vue'
import { compileToFunctions } from 'vue-template-compiler'
import mount from '~src/mount'

Expand Down Expand Up @@ -39,4 +40,10 @@ describe('hasClass', () => {
expect(fn).to.throw().with.property('message', message)
})
})

it('returns true when element contains class name mapped in css modules', () => {
const wrapper = mount(ComponentWithCssModules)

expect(wrapper.hasClass('color-red')).to.equal(true)
})
})