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:(image): add test #349

Merged
merged 39 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ea64917
feat:n-input Support hidden password
doom-9 Jun 17, 2021
149d8e6
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 17, 2021
8664169
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 18, 2021
0627777
feat(form): support require-mark-placement(#171)
doom-9 Jun 18, 2021
f9729c8
Revert "feat(form): support require-mark-placement(#171)"
doom-9 Jun 18, 2021
2566db7
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 18, 2021
aa41027
Revert "feat:n-input Support hidden password"
doom-9 Jun 18, 2021
3f01195
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 18, 2021
076c432
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 21, 2021
2307ef8
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 21, 2021
d4f08f8
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 21, 2021
56f6d75
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 22, 2021
658835d
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 22, 2021
3d90083
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 22, 2021
1e860cc
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 23, 2021
c1c7942
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 23, 2021
0cb198e
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 24, 2021
71ad4cd
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 24, 2021
cd8476a
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 25, 2021
ac74273
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 26, 2021
e685823
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 28, 2021
2a450f1
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 28, 2021
b02f5f8
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 29, 2021
4f151e7
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 29, 2021
b8cf341
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 29, 2021
c7fa132
Merge branch 'TuSimple:main' into main
doom-9-zz Jun 30, 2021
d7348e6
Merge branch 'TuSimple:main' into main
doom-9-zz Jul 1, 2021
c504c72
Merge branch 'TuSimple:main' into main
doom-9-zz Jul 1, 2021
c14f943
Merge branch 'TuSimple:main' into main
doom-9-zz Jul 1, 2021
23da897
Merge branch 'TuSimple:main' into main
doom-9-zz Jul 2, 2021
eb86273
Merge branch 'TuSimple:main' into main
doom-9-zz Jul 2, 2021
ba7fdda
Merge branch 'TuSimple:main' into main
doom-9-zz Jul 2, 2021
51430c2
Merge branch 'TuSimple:main' into main
doom-9-zz Jul 3, 2021
b67ea38
feat:(image): add test
doom-9 Jul 2, 2021
a10c17e
feat:(image): add test
doom-9 Jul 2, 2021
b93c886
WIP
doom-9 Jul 2, 2021
0c2f8a3
WIP
doom-9 Jul 2, 2021
295cc2c
feat(image): add imgProps prop and add test
doom-9-zz Jul 2, 2021
559d220
feat(image): add imgProps type
doom-9-zz Jul 3, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Feats

- `n-loading-bar` export `LoadingBarApi` type
- `n-image` add `imgProps` prop

## 2.15.2 (2021-07-02)

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Feats

- `n-loading-bar` 导出 `LoadingBarApi` 类型
- `n-image` 增加 `imgProps` 属性

## 2.15.2 (2021-07-02)

Expand Down
57 changes: 35 additions & 22 deletions src/image/src/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { defineComponent, h, inject, ref, PropType } from 'vue'
import { defineComponent, h, inject, ref, PropType, toRef } from 'vue'
import NImagePreview from './ImagePreview'
import type { ImagePreviewInst } from './ImagePreview'
import { imageGroupInjectionKey } from './ImageGroup'
import { ExtractPublicPropTypes } from '../../_utils'
import { useConfig } from '../../_mixins'

interface imgProps {
alt?: string
crossorigin?: 'anonymous' | 'use-credentials' | ''
decoding?: 'async' | 'auto' | 'sync'
height?: number
sizes?: string
src?: string
srcset?: string
usemap?: string
width?: number
}

const imageProps = {
alt: String,
width: [String, Number] as PropType<string | number>,
height: [String, Number] as PropType<string | number>,
imgProps: Object as PropType<imgProps>,
width: [String, Number] as PropType<string | number>,
src: String,
showToolbar: { type: Boolean, default: true }
}
Expand All @@ -20,6 +33,7 @@ export default defineComponent({
props: imageProps,
setup (props) {
const imageRef = ref<HTMLImageElement | null>(null)
const imgPropsRef = toRef(props, 'imgProps')
const previewInstRef = ref<ImagePreviewInst | null>(null)
const imageGroupHandle = inject(imageGroupInjectionKey, null)
const { mergedClsPrefixRef } = imageGroupHandle || useConfig(props)
Expand All @@ -28,6 +42,7 @@ export default defineComponent({
groupId: imageGroupHandle?.groupId,
previewInstRef,
imageRef,
imgProps: imgPropsRef,
handleClick: () => {
if (imageGroupHandle) {
imageGroupHandle.setPreviewSrc(props.src)
Expand All @@ -44,19 +59,25 @@ export default defineComponent({
}
},
render () {
const { mergedClsPrefix } = this
const { mergedClsPrefix, imgProps = {} } = this

const imgNode = (
<img
{...imgProps}
class={this.groupId}
ref="imageRef"
width={this.width ? this.width : imgProps.width}
height={this.height ? this.height : imgProps.height}
src={this.src ? this.src : imgProps.src}
alt={this.alt ? this.alt : imgProps.alt}
aria-label={this.alt ? this.alt : imgProps.alt}
onClick={this.handleClick}
/>
)

return this.groupId ? (
<div class={`${mergedClsPrefix}-image`} role="none">
<img
class={this.groupId}
ref="imageRef"
width={this.width}
height={this.height}
src={this.src}
alt={this.alt}
aria-label={this.alt}
onClick={this.handleClick}
/>
{imgNode}
</div>
) : (
<NImagePreview
Expand All @@ -68,15 +89,7 @@ export default defineComponent({
default: () => {
return (
<div class={`${mergedClsPrefix}-image`} role="none">
<img
ref="imageRef"
width={this.width}
height={this.height}
src={this.src}
alt={this.alt}
aria-label={this.alt}
onClick={this.handleClick}
/>
{imgNode}
</div>
)
}
Expand Down
67 changes: 67 additions & 0 deletions src/image/tests/Image.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { mount } from '@vue/test-utils'
import { NImage } from '../index'

describe('n-image', () => {
it('should work with import on demand', () => {
mount(NImage)
})

it('should work with `alt` prop', async () => {
const wrapper = mount(NImage)

await wrapper.setProps({ alt: 'This is just a picture' })

expect(wrapper.find('img').attributes('alt')).toBe('This is just a picture')
expect(wrapper.find('img').attributes('aria-label')).toBe(
'This is just a picture'
)
})

it('should work with `width` prop', async () => {
const wrapper = mount(NImage)

await wrapper.setProps({ width: '200' })

expect(wrapper.find('img').attributes('width')).toBe('200')

await wrapper.setProps({ width: 200 })

expect(wrapper.find('img').attributes('width')).toBe('200')
})

it('should work with `height` prop', async () => {
const wrapper = mount(NImage)

await wrapper.setProps({ height: '300' })

expect(wrapper.find('img').attributes('height')).toBe('300')

await wrapper.setProps({ height: 300 })

expect(wrapper.find('img').attributes('height')).toBe('300')
})

it('should work with `src` prop', async () => {
const wrapper = mount(NImage)

await wrapper.setProps({
src: 'https://www.naiveui.com/assets/naivelogo.93278402.svg'
})

expect(wrapper.find('img').attributes('src')).toBe(
'https://www.naiveui.com/assets/naivelogo.93278402.svg'
)
})

it('should work with `showToolbar` prop', async () => {
const wrapper = mount(NImage)

await wrapper.setProps({
showToolbar: true
})

await wrapper.find('img').trigger('click')

expect(document.querySelector('.n-image-preview-toolbar')).not.toEqual(null)
})
})