Skip to content

Commit

Permalink
add unit tests and little more cleanup of uncessary lines
Browse files Browse the repository at this point in the history
Signed-off-by: Devlin Junker <devlin.junker@gmail.com>
  • Loading branch information
devlinjunker authored and Grotax committed Sep 15, 2023
1 parent 7e21dc8 commit dce0701
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default Vue.extend({
async created() {
await this.$store.dispatch(ACTIONS.FETCH_FOLDERS)
await this.$store.dispatch(ACTIONS.FETCH_FEEDS)
await this.$store.dispatch(ACTIONS.FETCH_STARRED)
},
})
</script>
Expand Down
15 changes: 13 additions & 2 deletions src/components/feed-display/FeedItemDisplayList.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div style="height: 100%; display: flex; flex-direction: column;">
<div style="justify-content: right; display: flex">
<div class="feed-item-display-list">
<div class="header">
<NcActions class="filter-container" :force-menu="true">
<template #icon>
<FilterIcon />
Expand Down Expand Up @@ -180,6 +180,12 @@ export default Vue.extend({
</script>

<style scoped>
.feed-item-display-list {
height: 100%;
display: flex;
flex-direction: column;
}
.virtual-scroll {
border-top: 1px solid var(--color-border);
width: 100%;
Expand All @@ -196,6 +202,11 @@ export default Vue.extend({
height: calc(100vh - 50px - 50px - 10px)
}
.header {
justify-content: right;
display: flex;
}
.filter-container {
padding: 5px;
}
Expand Down
65 changes: 65 additions & 0 deletions tests/javascript/unit/components/routes/All.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Vuex, { Store } from 'vuex'
import { shallowMount, createLocalVue, Wrapper } from '@vue/test-utils'

import All from '../../../../../src/components/routes/All.vue'
import FeedItemDisplayList from '../../../../../src/components/feed-display/FeedItemDisplayList.vue'

jest.mock('@nextcloud/axios')

describe('All.vue', () => {
'use strict'
const localVue = createLocalVue()
localVue.use(Vuex)
let wrapper: Wrapper<All>

const mockItem = {
feedId: 1,
title: 'feed item',
pubDate: Date.now() / 1000,
}

let store: Store<any>
beforeAll(() => {
store = new Vuex.Store({
state: {
items: {
fetchingItems: {
all: false,
},
},
},
actions: {
},
getters: {
allItems: () => [mockItem, mockItem, mockItem],
},
})

store.dispatch = jest.fn()
store.commit = jest.fn()

wrapper = shallowMount(All, {
propsData: {
item: mockItem,
},
localVue,
store,
})
})

it('should get all items from state', () => {
expect((wrapper.findComponent(FeedItemDisplayList)).props().items.length).toEqual(3)
})

it('should dispatch FETCH_ITEMS action if not fetchingItems.all', () => {
(wrapper.vm as any).$store.state.items.fetchingItems.all = true;

(wrapper.vm as any).fetchMore()
expect(store.dispatch).not.toBeCalled();

(wrapper.vm as any).$store.state.items.fetchingItems.all = false;

(wrapper.vm as any).fetchMore()
expect(store.dispatch).toBeCalled()
})
})
89 changes: 89 additions & 0 deletions tests/javascript/unit/components/routes/Folder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Vuex, { Store } from 'vuex'
import { shallowMount, createLocalVue, Wrapper } from '@vue/test-utils'

import Folder from '../../../../../src/components/routes/Folder.vue'
import FeedItemDisplayList from '../../../../../src/components/feed-display/FeedItemDisplayList.vue'

jest.mock('@nextcloud/axios')

describe('Folder.vue', () => {
'use strict'
const localVue = createLocalVue()
localVue.use(Vuex)
let wrapper: Wrapper<Folder>

const mockFeed = {
id: 789,
title: 'feed name',
unreadCount: 2,
folderId: 123,
}

const mockFeed2 = {
id: 456,
title: 'feed name 2',
unreadCount: 2,
folderId: 123,
}

const mockFolder = {
id: 123,
name: 'folder name',
}

let store: Store<any>
beforeAll(() => {
store = new Vuex.Store({
state: {
items: {
fetchingItems: {
'folder-123': false,
},
allItems: [{
feedId: 789,
title: 'feed item',
}, {
feedId: 456,
title: 'feed item 2',
}],
},
},
actions: {
},
getters: {
feeds: () => [mockFeed, mockFeed2],
folders: () => [mockFolder],
},
})

store.dispatch = jest.fn()
store.commit = jest.fn()

wrapper = shallowMount(Folder, {
propsData: {
folderId: '123',
},
mocks: {
$route: {
params: {},
},
},
localVue,
store,
})
})

it('should display feed title and unread count', () => {
expect(wrapper.find('.header').text()).toContain(mockFolder.name)
expect(wrapper.find('.header').text()).toContain((mockFeed.unreadCount + mockFeed2.unreadCount).toString())
})

it('should get folder items from state', () => {
expect((wrapper.findComponent(FeedItemDisplayList)).props().items.length).toEqual(2)
})

it('should dispatch FETCH_FOLDER_FEED_ITEMS action on fetchMore', () => {
(wrapper.vm as any).fetchMore()
expect(store.dispatch).toBeCalled()
})
})
33 changes: 31 additions & 2 deletions tests/javascript/unit/services/item.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ describe('item.service.ts', () => {
(axios.post as any).mockReset()
})

describe('fetchAll', () => {
it('should call GET with offset set to start param, ALL item type', async () => {
(axios as any).get.mockResolvedValue({ data: { feeds: [] } })

await ItemService.fetchAll(0)

expect(axios.get).toBeCalled()
const queryParams = (axios.get as any).mock.calls[0][1].params

expect(queryParams.offset).toEqual(0)
expect(queryParams.type).toEqual(ITEM_TYPES.ALL)
})
})

describe('fetchStarred', () => {
it('should call GET with offset set to start param and STARRED item type', async () => {
(axios as any).get.mockResolvedValue({ data: { feeds: [] } })
Expand Down Expand Up @@ -40,7 +54,7 @@ describe('item.service.ts', () => {
})

describe('fetchFeedItems', () => {
it('should call GET with offset set to start param, UNREAD item type, and id set to feedId', async () => {
it('should call GET with offset set to start param, FEED item type, and id set to feedId', async () => {
(axios as any).get.mockResolvedValue({ data: { feeds: [] } })

await ItemService.fetchFeedItems(123, 0)
Expand All @@ -50,7 +64,22 @@ describe('item.service.ts', () => {

expect(queryParams.id).toEqual(123)
expect(queryParams.offset).toEqual(0)
expect(queryParams.type).toEqual(ITEM_TYPES.ALL)
expect(queryParams.type).toEqual(ITEM_TYPES.FEED)
})
})

describe('fetchFolderItems', () => {
it('should call GET with offset set to start param, FOLDER item type, and id set to folderId', async () => {
(axios as any).get.mockResolvedValue({ data: { feeds: [] } })

await ItemService.fetchFolderItems(123, 0)

expect(axios.get).toBeCalled()
const queryParams = (axios.get as any).mock.calls[0][1].params

expect(queryParams.id).toEqual(123)
expect(queryParams.offset).toEqual(0)
expect(queryParams.type).toEqual(ITEM_TYPES.FOLDER)
})
})

Expand Down
15 changes: 15 additions & 0 deletions tests/javascript/unit/store/item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ describe('item.ts', () => {
})
})

describe('FETCH_FOLDER_FEED_ITEMS', () => {
it('should call ItemService and commit items to state', async () => {
const mockItems = [{ id: 123, title: 'feed item' }]
const fetchMock = jest.fn()
fetchMock.mockResolvedValue({ data: { items: mockItems } })
ItemService.debounceFetchFolderFeedItems = fetchMock as any
const commit = jest.fn()

await (actions[FEED_ITEM_ACTION_TYPES.FETCH_FOLDER_FEED_ITEMS] as any)({ commit }, { feedId: 123 })

expect(fetchMock).toBeCalled()
expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_ITEMS, mockItems)
})
})

it('MARK_READ should call GET and commit returned feeds to state', async () => {
const item = { id: 1 }
const commit = jest.fn()
Expand Down

0 comments on commit dce0701

Please sign in to comment.