From e112ba8e1cbde6116f22327045468745ce6af88f Mon Sep 17 00:00:00 2001 From: Nathaniel Blackburn Date: Mon, 16 Oct 2017 20:10:27 +0100 Subject: [PATCH 1/5] Added support for $style in hasClass Checks the className against $style where available. --- src/wrappers/wrapper.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/wrappers/wrapper.js b/src/wrappers/wrapper.js index 5df404037..b6dca5459 100644 --- a/src/wrappers/wrapper.js +++ b/src/wrappers/wrapper.js @@ -102,6 +102,11 @@ export default class Wrapper implements BaseWrapper { throwError('wrapper.hasClass() must be passed a string') } + // if $style is available and has a matching className, use that instead. + if (this.vm && this.vm.$style && this.vm.$style[className]) { + className = this.vm.$style[className] + } + return !!(this.element && this.element.classList.contains(className)) } From 958e9e168fed84c6f2c7675a0f38387fde42f2d3 Mon Sep 17 00:00:00 2001 From: Nathaniel Blackburn Date: Mon, 16 Oct 2017 21:12:49 +0100 Subject: [PATCH 2/5] Reworked to not redefine variables --- src/wrappers/wrapper.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/wrappers/wrapper.js b/src/wrappers/wrapper.js index b6dca5459..bc9e4cdfa 100644 --- a/src/wrappers/wrapper.js +++ b/src/wrappers/wrapper.js @@ -98,16 +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') } - // if $style is available and has a matching className, use that instead. - if (this.vm && this.vm.$style && this.vm.$style[className]) { - className = this.vm.$style[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(className)) + return !!(this.element && this.element.classList.contains(targetClass)) } /** From 9117c5c08bfa77614f78d285c3c0fda3d694a7c3 Mon Sep 17 00:00:00 2001 From: Nathaniel Blackburn Date: Tue, 17 Oct 2017 11:09:25 +0100 Subject: [PATCH 3/5] Added unit test --- .../components/component-with-css-modules.vue | 15 +++++++++++++++ test/unit/specs/mount/Wrapper/hasClass.spec.js | 7 +++++++ 2 files changed, 22 insertions(+) create mode 100644 test/resources/components/component-with-css-modules.vue diff --git a/test/resources/components/component-with-css-modules.vue b/test/resources/components/component-with-css-modules.vue new file mode 100644 index 000000000..3ac044326 --- /dev/null +++ b/test/resources/components/component-with-css-modules.vue @@ -0,0 +1,15 @@ + + + + + diff --git a/test/unit/specs/mount/Wrapper/hasClass.spec.js b/test/unit/specs/mount/Wrapper/hasClass.spec.js index d5851d002..ee05d1c0c 100644 --- a/test/unit/specs/mount/Wrapper/hasClass.spec.js +++ b/test/unit/specs/mount/Wrapper/hasClass.spec.js @@ -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' @@ -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) + }) }) From 761f0345b8136f74e1d6575038d732d7cdb59fad Mon Sep 17 00:00:00 2001 From: Nathaniel Blackburn Date: Tue, 17 Oct 2017 12:05:57 +0100 Subject: [PATCH 4/5] Linting fixes --- src/wrappers/wrapper.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wrappers/wrapper.js b/src/wrappers/wrapper.js index bc9e4cdfa..96a4ac8d4 100644 --- a/src/wrappers/wrapper.js +++ b/src/wrappers/wrapper.js @@ -98,15 +98,15 @@ export default class Wrapper implements BaseWrapper { * Asserts wrapper has a class name */ hasClass (className: string) { - let targetClass = className; + let targetClass = className if (typeof targetClass !== 'string') { throwError('wrapper.hasClass() must be passed a string') } - // 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]; + // 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)) From 2b6a81b9e5fe9339e079da32abc95b0f5e3f5a94 Mon Sep 17 00:00:00 2001 From: Nathaniel Blackburn Date: Tue, 17 Oct 2017 12:06:12 +0100 Subject: [PATCH 5/5] Fixed unit test --- test/resources/components/component-with-css-modules.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/resources/components/component-with-css-modules.vue b/test/resources/components/component-with-css-modules.vue index 3ac044326..946af93fe 100644 --- a/test/resources/components/component-with-css-modules.vue +++ b/test/resources/components/component-with-css-modules.vue @@ -1,5 +1,5 @@