-
Notifications
You must be signed in to change notification settings - Fork 63
Move useLoadMore functionality in the Load More component #1238
Changes from all commits
f9647fb
a6e3cad
86f01f7
e8f33b7
4d82e1b
a1a633b
486856c
f37450a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
<template> | ||
<VButton | ||
v-show="canLoadMore" | ||
size="large" | ||
variant="full" | ||
type="button" | ||
:disabled="isFetching" | ||
data-testid="load-more" | ||
@click="onLoadMore" | ||
> | ||
{{ buttonLabel }} | ||
|
@@ -12,33 +13,43 @@ | |
<script lang="ts"> | ||
import { computed, defineComponent, useContext } from '@nuxtjs/composition-api' | ||
|
||
import { useMediaStore } from '~/stores/media' | ||
import { useSearchStore } from '~/stores/search' | ||
|
||
import VButton from '~/components/VButton.vue' | ||
|
||
export default defineComponent({ | ||
name: 'VLoadMore', | ||
components: { | ||
VButton, | ||
}, | ||
props: { | ||
isFetching: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
}, | ||
setup(_, { emit }) { | ||
setup() { | ||
const { i18n } = useContext() | ||
const mediaStore = useMediaStore() | ||
const searchStore = useSearchStore() | ||
|
||
const buttonLabel = computed(() => { | ||
return i18n.t('browse-page.load') | ||
}) | ||
const canLoadMore = computed( | ||
() => | ||
searchStore.searchTerm !== '' && | ||
mediaStore.fetchState.canFetch && | ||
mediaStore.resultCount > 0 | ||
) | ||
const onLoadMore = async () => { | ||
if (!canLoadMore.value) return | ||
|
||
const onLoadMore = () => { | ||
emit('onLoadMore') | ||
await mediaStore.fetchMedia({ | ||
shouldPersistMedia: true, | ||
}) | ||
} | ||
const isFetching = computed(() => mediaStore.fetchState.isFetching) | ||
|
||
const buttonLabel = computed(() => i18n.t('browse-page.load')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that it was already this way, but why does this need to be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it doesn't 😆 I think I wrote it that way when I was only starting to learn the way Composition API worked. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to putting this directly on the template 🙂 |
||
|
||
return { | ||
buttonLabel, | ||
isFetching, | ||
onLoadMore, | ||
canLoadMore, | ||
} | ||
}, | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { render, screen } from '@testing-library/vue' | ||
import VueI18n from 'vue-i18n' | ||
import { createPinia, PiniaVuePlugin } from 'pinia' | ||
import { createLocalVue } from '@vue/test-utils' | ||
|
||
import messages from '~/locales/en.json' | ||
|
||
|
@@ -17,43 +19,34 @@ const propsData = { | |
{ id: 'i2', url: 'http://localhost:8080/i2.jpg', title: 'image2' }, | ||
{ id: 'i3', url: 'http://localhost:8080/i3.svg', title: 'image3' }, | ||
], | ||
canLoadMore: false, | ||
fetchState: { | ||
isFetching: false, | ||
fetchingError: null, | ||
}, | ||
} | ||
const options = { | ||
props: propsData, | ||
stubs: ['NuxtLink', 'VLicense'], | ||
mocks: { | ||
$nuxt: { | ||
context: { | ||
i18n, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
describe('VImageGrid', () => { | ||
it('renders images without load more button if canLoadMore is false', () => { | ||
options.props.canLoadMore = false | ||
render(VImageGrid, options) | ||
expect(screen.queryAllByRole('img').length).toEqual(propsData.images.length) | ||
expect(screen.queryAllByRole('figure').length).toEqual( | ||
propsData.images.length | ||
) | ||
expect(screen.queryByTestId('load-more')).not.toBeInTheDocument() | ||
let localVue | ||
let pinia | ||
let options | ||
beforeEach(() => { | ||
localVue = createLocalVue() | ||
localVue.use(PiniaVuePlugin) | ||
pinia = createPinia() | ||
options = { | ||
localVue, | ||
pinia, | ||
props: propsData, | ||
stubs: ['NuxtLink', 'VLicense'], | ||
mocks: { $nuxt: { context: { i18n } } }, | ||
} | ||
}) | ||
|
||
it('renders images and load more button if canLoadMore is true', async () => { | ||
options.props.canLoadMore = true | ||
it('renders images without load more button', () => { | ||
render(VImageGrid, options) | ||
expect(screen.queryAllByRole('img').length).toEqual(propsData.images.length) | ||
expect(screen.queryAllByRole('figure').length).toEqual( | ||
propsData.images.length | ||
) | ||
const loadMoreButton = screen.queryByTestId('load-more') | ||
expect(loadMoreButton).toBeVisible() | ||
expect(loadMoreButton).toHaveTextContent(messages['browse-page'].load) | ||
expect(screen.queryByTestId('load-more')).not.toBeVisible() | ||
Comment on lines
+44
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test file now misses the other side of it, when the load more button does show. Also I think there are three branches to test for when load more shows or not, ideally we'd test those in a VLoadMore specific test file: searchStore.searchTerm !== '' &&
mediaStore.fetchState.canFetch &&
mediaStore.resultCount > 0 Each of those is a distinct branch we'd want to test 😕 Then the |
||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary or could we test just using the label? Generally it's preferable to avoid introducing test id's if possible, in this case a
getByText
should be okay, I think.