Created
September 28, 2018 09:49
-
-
Save yarkovaleksei/4e54a298c4fd8413e9e7e7b6c6fe2253 to your computer and use it in GitHub Desktop.
Highlight component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { mount } from '@vue/test-utils' | |
import Highlight from './Highlight.vue' | |
let wrapper: any | |
describe('ui/Highlight', () => { | |
beforeEach(() => { | |
wrapper = mount(Highlight, { | |
propsData: { | |
search: '', | |
message: '' | |
}, | |
attachToDocument: true | |
}) | |
}) | |
it('should render correct contents', () => { | |
wrapper.setProps({ | |
message: 'search' | |
}) | |
expect(wrapper.findAll('span > span')).toHaveLength(11) | |
}) | |
it('should correct computed "parsedMsg"', () => { | |
wrapper.setProps({ | |
message: 'search' | |
}) | |
expect(wrapper.vm.parsedMsg.length).toEqual(11) | |
expect(wrapper.vm.parsedMsg).toEqual(['s', '', 'e', '', 'a', '', 'r', '', 'c', '', 'h']) | |
}) | |
it('should correct method "getClass(i: number)"', () => { | |
expect(wrapper.vm.getClass(0)).toEqual({ highlight: false }) | |
expect(wrapper.vm.getClass(1)).toEqual({ highlight: true }) | |
}) | |
it('should correct computed "parsedSearch"', () => { | |
wrapper.setProps({ | |
search: 'sea' | |
}) | |
expect(wrapper.vm.parsedSearch).toEqual('(sea)') | |
}) | |
it('should render correct search result', () => { | |
wrapper.setProps({ | |
search: 'sea', | |
message: 'search' | |
}) | |
const result = wrapper.findAll('span > span') | |
expect(result.at(0).html()).toEqual('<span class=""></span>') | |
expect(result.at(1).html()).toEqual('<span class="highlight">sea</span>') | |
expect(result.at(2).html()).toEqual('<span class="">rch</span>') | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<span> | |
<span v-for="(search, i) in parsedMsg" | |
:key="i" | |
:class="getClass(i%2)" | |
v-html="search"></span> | |
</span> | |
</template> | |
<script lang="ts"> | |
import script from './script' | |
export default script | |
</script> | |
<style lang="scss" scoped> | |
.highlight { | |
background-color: #fff5ac; | |
} | |
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Vue, Component, Prop } from 'vue-property-decorator' | |
@Component({}) | |
export default class Highlight extends Vue { | |
@Prop({ type: String, default: '' }) | |
search: string | |
@Prop({ type: String, default: '' }) | |
message: string | |
get parsedSearch(): string { | |
return `(${this.search.trim().replace(/ +/g, ' |')})` | |
} | |
get parsedMsg(): Array<string> { | |
return this.message.split(new RegExp(this.parsedSearch, 'gi')) | |
} | |
getClass(i: number): object { | |
const myClass = { | |
highlight: !!i | |
} | |
return myClass | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment