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

refactor(avatar): test and core code #322

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Changes from 1 commit
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
151 changes: 151 additions & 0 deletions packages/components/avatar/src/avatar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { enableAutoUnmount, mount } from '@vue/test-utils'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import type { VueWrapper } from '@vue/test-utils'
import { axe } from 'vitest-axe'
import { OkuAvatar, OkuAvatarFallback, OkuAvatarImage } from '@oku-ui//avatar'

const AVATAR_TEST_ID = 'avatar-test'
const FALLBACK_TEXT = 'AB'
const IMAGE_ALT_TEXT = 'Fake Avatar'
const DELAY = 300

enableAutoUnmount(afterEach)

/**
* @vitest-environment jsdom
*/

describe('given an Avatar with fallback and no image', () => {
let wrapper: VueWrapper

beforeEach(() => {
wrapper = mount({
components: {
OkuAvatar,
OkuAvatarFallback,
},
setup() {
return {
AVATAR_TEST_ID,
FALLBACK_TEXT,
}
},
template: `
<OkuAvatar :data-testid="AVATAR_TEST_ID">
<OkuAvatarFallback>{{ FALLBACK_TEXT }}</OkuAvatarFallback>
Cr0zy07 marked this conversation as resolved.
Show resolved Hide resolved
</OkuAvatar>
`,
})
})

it('should have no accessibility violations', async () => {
// https://github.com/capricorn86/happy-dom/issues/978
// TODO:77 https://github.com/chaance/vitest-axe/issues/7
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(await axe(wrapper.element)).toHaveNoViolations()
})
})

describe('given an Avatar with fallback and a working image', () => {
let wrapper: VueWrapper
const originalGlobalImage = window.Image

beforeAll(() => {
(window.Image as any) = class MockImage {
onload: () => void = () => {}
src: string = ''
constructor() {
setTimeout(() => {
this.onload()
}, DELAY)
return this
}
}
})

afterAll(() => {
window.Image = originalGlobalImage
})

beforeEach(() => {
wrapper = mount({
components: {
OkuAvatar,
OkuAvatarFallback,
OkuAvatarImage,
},
setup() {
return {
AVATAR_TEST_ID,
FALLBACK_TEXT,
IMAGE_ALT_TEXT,
}
},
template: `
<OkuAvatar :data-testid="AVATAR_TEST_ID">
<OkuAvatarFallback>{{ FALLBACK_TEXT }}</OkuAvatarFallback>
<OkuAvatarImage src="https://picsum.photos/id/1005/400/400" :alt="IMAGE_ALT_TEXT" />
</OkuAvatar>
`,
})
})

it('should render the fallback initially', () => {
expect(wrapper.text()).toContain(FALLBACK_TEXT)
})

it('should not render the image initially', () => {
expect(wrapper.find('img').exists()).toBe(false)
})

it('should render the image after it has loaded', async () => {
await new Promise(resolve => setTimeout(resolve, DELAY))

expect(wrapper.find('img').exists()).toBe(true)
})

it('should have alt text on the image', async () => {
await new Promise(resolve => setTimeout(resolve, DELAY))

expect(wrapper.find('img').attributes('alt')).toBe(IMAGE_ALT_TEXT)
})
})

describe('given an Avatar with fallback and delayed render', () => {
let wrapper: VueWrapper

beforeEach(() => {
wrapper = mount({
components: {
OkuAvatar,
OkuAvatarFallback,
},
setup() {
return {
AVATAR_TEST_ID,
FALLBACK_TEXT,
IMAGE_ALT_TEXT,
DELAY,
}
},
template: `
<OkuAvatar :data-testid="AVATAR_TEST_ID">
<OkuAvatarFallback :delayMs="DELAY">{{ FALLBACK_TEXT }}</OkuAvatarFallback>
</OkuAvatar>
`,
})
})

it('should not render a fallback immediately', () => {
expect(wrapper.text()).not.toContain(FALLBACK_TEXT)
})

it('should render a fallback after the delay', async () => {
expect(wrapper.text()).not.toContain(FALLBACK_TEXT)

await new Promise(resolve => setTimeout(resolve, DELAY))

expect(wrapper.text()).toContain(FALLBACK_TEXT)
})
})
Loading