Skip to content

Commit de89293

Browse files
nblackburneddyerburgh
authored andcommitted
feat: add support for CSS modules in hasClass (#106)
* Added support for $style in hasClass Checks the className against $style where available. * Reworked to not redefine variables * Added unit test * Linting fixes * Fixed unit test
1 parent 020fc4d commit de89293

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/wrappers/wrapper.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,18 @@ export default class Wrapper implements BaseWrapper {
9898
* Asserts wrapper has a class name
9999
*/
100100
hasClass (className: string) {
101-
if (typeof className !== 'string') {
101+
let targetClass = className
102+
103+
if (typeof targetClass !== 'string') {
102104
throwError('wrapper.hasClass() must be passed a string')
103105
}
104106

105-
return !!(this.element && this.element.classList.contains(className))
107+
// if $style is available and has a matching target, use that instead.
108+
if (this.vm && this.vm.$style && this.vm.$style[targetClass]) {
109+
targetClass = this.vm.$style[targetClass]
110+
}
111+
112+
return !!(this.element && this.element.classList.contains(targetClass))
106113
}
107114

108115
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<div :class="$style['color-red']"></div>
3+
</template>
4+
5+
<style module>
6+
.color-red {
7+
color: red;
8+
}
9+
</style>
10+
11+
<script>
12+
export default{
13+
name: 'component-with-css-modules'
14+
}
15+
</script>

test/unit/specs/mount/Wrapper/hasClass.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ComponentWithCssModules from '~resources/components/component-with-css-modules.vue'
12
import { compileToFunctions } from 'vue-template-compiler'
23
import mount from '~src/mount'
34

@@ -39,4 +40,10 @@ describe('hasClass', () => {
3940
expect(fn).to.throw().with.property('message', message)
4041
})
4142
})
43+
44+
it('returns true when element contains class name mapped in css modules', () => {
45+
const wrapper = mount(ComponentWithCssModules)
46+
47+
expect(wrapper.hasClass('color-red')).to.equal(true)
48+
})
4249
})

0 commit comments

Comments
 (0)