Skip to content
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

feat: add select color scheme tests #78

Open
wants to merge 1 commit into
base: proto
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
setupFilesAfterEnv: ['<rootDir>/tests/unit/setup.ts'],
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel'
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
setupFilesAfterEnv: ['<rootDir>/tests/unit/setup.ts']
}
8 changes: 8 additions & 0 deletions src/components/Profile/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
</header>
</template>

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
name: 'HeaderProfile'
})
</script>

<style lang="scss">
.profile-header {
width: 100%;
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/__helpers__/stubComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function stubComponent(Component, options = {}) {
return {
props: Component.options.props,
model: Component.options.model,
// Do not render any slots/scoped slots except default
// This differs from VTU behavior which renders all slots
template: '<div><slot></slot></div>',
// allows wrapper.find(Component) to work for stub
$_vueTestUtils_original: Component,
...options
}
}
89 changes: 89 additions & 0 deletions tests/unit/components/Profile/SelectColorScheme.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import SelectColorScheme from '@/components/Profile/SelectColorScheme.vue'
import SelectColorSchemeCard from '@/components/Profile/SelectColorSchemeCard.vue'
import HeaderProfile from '@/components/Profile/Header.vue'
import SwapHelpText from '@/components/UI/SwapHelpText.vue'
import SwapButton from '@/components/UI/SwapButton.vue'

import { shallowMount } from '@vue/test-utils'
import { mockColorScheme1, mockColorScheme2 } from '../../__mocks__/userColorScheme.mock'
import { stubComponent } from '../../__helpers__/stubComponent'

const HeaderProfileStub = stubComponent(HeaderProfile, {
template: `
<div>
<slot></slot>
<slot name="help-text"></slot>
</div>
`
})

const SwapHelpTextStub = stubComponent(SwapHelpText, {
template: `
<div>
<slot></slot>
</div>
`
})

describe('SelectColorScheme', () => {
let wrapper

const createComponent = ({ propsData = {}, provide = {} } = {}) => {
wrapper = shallowMount(SelectColorScheme, {
propsData: {
...propsData
},
provide: {
...provide
},
stubs: {
HeaderProfile: HeaderProfileStub,
SwapHelpText: SwapHelpTextStub
}
})
}

const findButtonByText = text => {
return wrapper
.findAllComponents(SwapButton)
.filter(buttonWrapper => buttonWrapper.text().includes(text))
.at(0)
}

beforeEach(() => {
createComponent({ propsData: { options: [mockColorScheme1, mockColorScheme2] } })
})

it('renders help text', () => {
expect(wrapper.html().includes('Your own colors will protect you from phishing.')).toBe(true)
})

it('emits update color scheme', () => {
const cardWrapper = wrapper.findComponent(SelectColorSchemeCard)
cardWrapper.vm.$emit('select')

expect(wrapper.emitted()['select-color-scheme']).toBeTruthy()
})

it('emits refresh color scheme', () => {
const refreshButton = findButtonByText('Refresh colors')
refreshButton.vm.$emit('click')

expect(wrapper.emitted()['refresh-color-schemes']).toBeTruthy()
})

it('emits update select colors scheme', () => {
const cardWrapper = wrapper.findComponent(SelectColorSchemeCard)
cardWrapper.vm.$emit('select')

const emitted = wrapper.emitted()['update:selected-color-scheme'][0][0]

expect(emitted).toEqual(mockColorScheme1)
})

it('renders all options', () => {
const cardWrappers = wrapper.findAllComponents(SelectColorSchemeCard)

expect(cardWrappers.length).toBe(2)
})
})
7 changes: 7 additions & 0 deletions tests/unit/setup.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
import Vue from 'vue'
import UI from '@/components/UI'
import { enableAutoDestroy } from '@vue/test-utils'

process.env.VUE_APP_HOME_URL = 'http://localhost'
process.env.VUE_APP_KEYS_URL = 'http://keys.localhost'

Vue.use(UI)
enableAutoDestroy(global.afterEach)