-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test to storybook utils (#1121)
* chore: add test to storybook utils * chore: hit 100% test coverage Co-authored-by: Lee Chase <lee.chase@uk.ibm.com>
- Loading branch information
Showing
15 changed files
with
323 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
68 changes: 68 additions & 0 deletions
68
packages/v3/src/components/CvButton/__tests__/CvButtonCommon.spec.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { carbonPrefix } from '../../../global/settings'; | ||
import { props, useCvButtonCommon } from '../CvButtonCommon'; | ||
import { CvButtonConsts } from '..'; | ||
|
||
describe('CvButtonCommon.props', () => { | ||
it('has no default icon', () => { | ||
expect(props.icon.default).not.toBeDefined(); | ||
}); | ||
|
||
it('has a default kind of primary', () => { | ||
expect(props.kind.default === 'primary').toBe(true); | ||
}); | ||
|
||
it('validates kind', () => { | ||
const spy = jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
const validator = props.kind.validator; | ||
CvButtonConsts.buttonKinds.forEach(kind => { | ||
expect(validator(kind)).toBe(true); | ||
}); | ||
expect(spy).not.toBeCalled(); | ||
|
||
expect(validator('banana is not a valid kind')).toBe(false); | ||
expect(spy).toBeCalled(); | ||
|
||
spy.mockRestore(); // Remove mock | ||
}); | ||
|
||
it('validates size', () => { | ||
const spy = jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
const validator = props.size.validator; | ||
CvButtonConsts.buttonSizes.forEach(size => { | ||
expect(validator(size)).toBe(true); | ||
}); | ||
expect(spy).not.toBeCalled(); | ||
|
||
expect(validator('banana is not a valid size')).toBe(false); | ||
expect(spy).toBeCalled(); | ||
|
||
spy.mockRestore(); // Remove mock | ||
}); | ||
}); | ||
|
||
describe('CvButtonCommon.useCvButtonCommon', () => { | ||
let { buttonClasses } = useCvButtonCommon('primary', 'sm', false, true); | ||
expect(buttonClasses.value).toEqual([ | ||
`${carbonPrefix}--btn`, | ||
`${carbonPrefix}--btn--icon-only`, | ||
`${carbonPrefix}--btn--primary`, | ||
`${carbonPrefix}--btn--sm`, | ||
]); | ||
|
||
buttonClasses = useCvButtonCommon('wibble', 'sm', true, true).buttonClasses; | ||
expect(buttonClasses.value).toEqual([ | ||
`${carbonPrefix}--btn`, | ||
`${carbonPrefix}--skeleton`, | ||
`${carbonPrefix}--btn--icon-only`, | ||
`${carbonPrefix}--btn--sm`, | ||
]); | ||
|
||
buttonClasses = useCvButtonCommon('secondary', 'small').buttonClasses; | ||
expect(buttonClasses.value).toEqual([ | ||
`${carbonPrefix}--btn`, | ||
`${carbonPrefix}--btn--secondary`, | ||
`${carbonPrefix}--btn--sm`, | ||
]); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/v3/src/components/CvButton/__tests__/CvIconButton.spec.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { shallowMount } from '@vue/test-utils'; | ||
import { carbonPrefix } from '../../../global/settings'; | ||
|
||
import { CvIconButton } from '..'; | ||
|
||
describe('CvIconButton', () => { | ||
it('renders button and slot', () => { | ||
const labelContent = 'label content'; | ||
const wrapper = shallowMount(CvIconButton, { | ||
props: { | ||
label: labelContent, | ||
}, | ||
}); | ||
|
||
const button = wrapper.find('button'); | ||
expect(button.classes()).toContain(`${carbonPrefix}--btn`); | ||
expect(button.classes()).toContain(`${carbonPrefix}--btn--primary`); | ||
|
||
const assistiveText = button.find(`.${carbonPrefix}--assistive-text`); | ||
expect(assistiveText.text()).toBe(labelContent); | ||
}); | ||
|
||
it('Raises click event when clicked', async () => { | ||
const wrapper = shallowMount(CvIconButton); | ||
await wrapper.find('button').trigger('click'); | ||
expect(wrapper.emitted('click')).toHaveLength(1); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { shallowMount } from '@vue/test-utils'; | ||
|
||
import CvSvg from '../_CvSvg'; | ||
import TestIcon from './_TestIcon'; | ||
|
||
const notAComponent = {}; | ||
const fakeComponentVue = { render: 'Vue 2' }; | ||
const svgInline = /* html */ `<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" id="root"> <g fill-rule="evenodd" id="g"> <path id="path" d="M7 7H4v2h3v3h2V9h3V7H9V4H7v3zm1 9A8 8 0 1 1 8 0a8 8 0 0 1 0 16z"/> </g> </svg>`; | ||
const svgSymbol = './test-svg.svg#test-symbol'; | ||
const svgFie = './test-svg.svg'; | ||
|
||
describe('Svg with fake icon', () => { | ||
it('renders the SVG from an icon component', () => { | ||
const wrapper = shallowMount(CvSvg, { | ||
props: { svg: TestIcon }, | ||
}); | ||
|
||
expect(wrapper.find('svg')).not.toBeNull(); | ||
}); | ||
|
||
it('renders the SVG from an svg file', () => { | ||
const wrapper = shallowMount(CvSvg, { | ||
props: { svg: svgFie }, | ||
}); | ||
|
||
expect(wrapper.find('svg')).not.toBeNull(); | ||
}); | ||
|
||
it('renders the SVG symbol', () => { | ||
const wrapper = shallowMount(CvSvg, { | ||
props: { svg: svgSymbol }, | ||
}); | ||
|
||
expect(wrapper.find('svg')).not.toBeNull(); | ||
}); | ||
|
||
it('renders the SVG inline', () => { | ||
const wrapper = shallowMount(CvSvg, { | ||
props: { svg: svgInline }, | ||
}); | ||
|
||
expect(wrapper.find('svg')).not.toBeNull(); | ||
}); | ||
|
||
it('validates icon', () => { | ||
const spy = jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
const validator = CvSvg.props.svg.validator; | ||
expect(validator(fakeComponentVue)).toBe(true); | ||
expect(spy).not.toBeCalled(); | ||
|
||
expect(validator(notAComponent)).toBe(false); | ||
expect(spy).toBeCalledTimes(1); | ||
|
||
spy.mockRestore(); // Remove mock | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<template> | ||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" id="root"> | ||
<g fill-rule="evenodd" id="g"> | ||
<path | ||
id="path" | ||
d="M7 7H4v2h3v3h2V9h3V7H9V4H7v3zm1 9A8 8 0 1 1 8 0a8 8 0 0 1 0 16z" | ||
/> | ||
</g> | ||
</svg> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'TestIcon', | ||
// setup() { | ||
// // empty setup | ||
// }, | ||
}; | ||
</script> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { storybookControlsFromProps } from '../storybook-utils'; | ||
|
||
const props = { | ||
boolean_0: true, | ||
boolean_1: Boolean, | ||
boolean_2: { type: Boolean }, | ||
boolean_3: { type: Boolean, default: true }, | ||
boolean_4: { default: true }, | ||
string_0: 'test', | ||
string_1: String, | ||
string_2: { type: String }, | ||
string_3: { type: String, default: 'test' }, | ||
string_4: { default: 'test' }, | ||
number_0: 1, | ||
number_1: Number, | ||
number_2: { type: Number }, | ||
number_3: { type: Number, default: 1 }, | ||
number_4: { default: 1 }, | ||
errNotSure: {}, | ||
errNotSure2: { type: {} }, | ||
}; | ||
|
||
const expectedResult = { | ||
boolean_0: { control: { type: 'boolean', default: true } }, | ||
boolean_1: { control: { type: 'boolean', default: false } }, | ||
boolean_2: { control: { type: 'boolean', default: false } }, | ||
boolean_3: { control: { type: 'boolean', default: true } }, | ||
boolean_4: { control: { type: 'boolean', default: true } }, | ||
string_0: { control: { type: 'text', default: 'test' } }, | ||
string_1: { control: { type: 'text', default: '' } }, | ||
string_2: { control: { type: 'text', default: '' } }, | ||
string_3: { control: { type: 'text', default: 'test' } }, | ||
string_4: { control: { type: 'text', default: 'test' } }, | ||
number_0: { control: { type: 'number', default: 1 } }, | ||
number_1: { control: { type: 'number', default: 0 } }, | ||
number_2: { control: { type: 'number', default: 0 } }, | ||
number_3: { control: { type: 'number', default: 1 } }, | ||
number_4: { control: { type: 'number', default: 1 } }, | ||
errNotSure: { control: { type: 'text', default: '' } }, | ||
errNotSure2: { control: { type: 'text', default: '' } }, | ||
}; | ||
|
||
describe('global/storybook-utils', () => { | ||
it('Should convert common props to storybook control types', () => { | ||
const controls = storybookControlsFromProps(props); | ||
|
||
expect(controls).toEqual(expectedResult); | ||
}); | ||
}); |
Oops, something went wrong.