Skip to content

Commit

Permalink
Added unit tests for file list
Browse files Browse the repository at this point in the history
  • Loading branch information
seenanair committed Dec 17, 2024
1 parent 0cdd42d commit 79b5753
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions app/frontend/javascript/file-list/components/FileList.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { mount } from '@vue/test-utils'
import FileList from './FileList.vue'

describe('FileList.vue', () => {
let wrapper

beforeEach(() => {
wrapper = mount(FileList, {
data() {
return {
qc_files: [],
loading: true,
}
},
})
})

it('renders loading spinner when loading is true', () => {
expect(wrapper.find('.spinner-dark').exists()).toBe(true)
})

it('does not render loading spinner when loading is false', async () => {
await wrapper.setData({ loading: false })
expect(wrapper.find('.spinner-dark').exists()).toBe(false)
})

it('renders "No files attached" when noFiles is true', async () => {
await wrapper.setData({ loading: false, qc_files: [] })
expect(wrapper.find('.list-group-item').text()).toBe('No files attached')
})

it('renders qc_files when they are present', async () => {
const mockFiles = [
{ uuid: '1', filename: 'file1.txt', created: '2023-01-01' },
{ uuid: '2', filename: 'file2.txt', created: '2023-01-02' },
]
await wrapper.setData({ loading: false, qc_files: mockFiles })
const fileLinks = wrapper.findAll('.list-group-item')
expect(fileLinks.length).toBe(2)
expect(fileLinks.at(0).text()).toBe('file1.txt - 2023-01-01')
expect(fileLinks.at(1).text()).toBe('file2.txt - 2023-01-02')
})

it('computed noFiles returns true when qc_files is empty and loading is false', async () => {
await wrapper.setData({ loading: false, qc_files: [] })
expect(wrapper.vm.noFiles).toBe(true)
})

it('computed noFiles returns false when qc_files is not empty', async () => {
await wrapper.setData({ loading: false, qc_files: [{ uuid: '1', filename: 'file1.txt', created: '2023-01-01' }] })
expect(wrapper.vm.noFiles).toBe(false)
})

it('calls fetchData on mount', () => {
const fetchDataSpy = vi.spyOn(FileList.methods, 'fetchData')
mount(FileList)
expect(fetchDataSpy).toHaveBeenCalled()
})
})

0 comments on commit 79b5753

Please sign in to comment.