Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>
  • Loading branch information
SwikritiT committed Aug 2, 2023
1 parent 8558e07 commit e8c209d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
10 changes: 7 additions & 3 deletions lib/Controller/OpenProjectAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ public function getNotifications(): DataResponse {
*
* @return DataResponse
*/
public function getSearchedWorkPackages(?string $searchQuery = null, ?int $fileId = null): DataResponse {
public function getSearchedWorkPackages(?string $searchQuery = null, ?int $fileId = null, bool $isSmartPicker = false): DataResponse {
if ($this->accessToken === '') {
return new DataResponse('', Http::STATUS_UNAUTHORIZED);
} elseif (!OpenProjectAPIService::validateURL($this->openprojectUrl)) {
return new DataResponse('', Http::STATUS_BAD_REQUEST);
}

$onlyLinkableWorkPackages = true;
if ($isSmartPicker) {
$onlyLinkableWorkPackages = false;
}
$result = $this->openprojectAPIService->searchWorkPackage(
$this->userId,
$searchQuery,
$fileId
$fileId,
$onlyLinkableWorkPackages
);

if (!isset($result['error'])) {
Expand Down
13 changes: 5 additions & 8 deletions src/components/tab/SearchInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,9 @@ export default {
},
async asyncFind(query) {
this.resetState()
if (this.isSmartPicker) {
await this.debounceMakeSearchRequest(query)
} else {
await this.debounceMakeSearchRequest(query, this.fileInfo.id)
}
await this.debounceMakeSearchRequest(query, this.fileInfo.id, this.isSmartPicker)
},
async getFileLink(selectedOption) {
async getWorkPackageLink(selectedOption) {
return this.openprojectUrl + '/projects/' + selectedOption.projectId + '/work_packages/' + selectedOption.id
},
debounceMakeSearchRequest: debounce(function(...args) {
Expand All @@ -147,7 +143,7 @@ export default {
}, DEBOUNCE_THRESHOLD),
async linkWorkPackageToFile(selectedOption) {
if (this.isSmartPicker) {
const link = await this.getFileLink(selectedOption)
const link = await this.getWorkPackageLink(selectedOption)
this.$emit('submit', link)
return
}
Expand Down Expand Up @@ -176,12 +172,13 @@ export default {
)
}
},
async makeSearchRequest(search, fileId = null) {
async makeSearchRequest(search, fileId = null, isSmartPicker = false) {
this.state = STATE.LOADING
const url = generateUrl('/apps/integration_openproject/work-packages')
const req = {}
req.params = {
searchQuery: search,
isSmartPicker,
}
let response
try {
Expand Down
53 changes: 51 additions & 2 deletions tests/jest/components/tab/SearchInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import workPackagesSearchResponseNoAssignee from '../../fixtures/workPackagesSea
import workPackageSearchReqResponse from '../../fixtures/workPackageSearchReqResponse.json'
import workPackageObjectsInSearchResults from '../../fixtures/workPackageObjectsInSearchResults.json'
import { STATE } from '../../../../src/utils.js'
import * as initialState from "@nextcloud/initial-state";
import * as initialState from '@nextcloud/initial-state'

jest.mock('@nextcloud/axios')
jest.mock('@nextcloud/dialogs')
Expand All @@ -24,13 +24,13 @@ jest.mock('lodash/debounce', () =>
})
)

// eslint-disable-next-line no-import-assign,import/namespace
initialState.loadState = jest.fn(() => {
return {
openproject_instance_url: null,
}
})


global.t = (app, text) => text

const localVue = createLocalVue()
Expand Down Expand Up @@ -151,6 +151,7 @@ describe('SearchInput.vue', () => {
{
params: {
searchQuery: 'orga',
isSmartPicker: false,
},
},
)
Expand Down Expand Up @@ -495,7 +496,55 @@ describe('SearchInput.vue', () => {
})
})
})

describe('search with smartpicker', () => {
let axiosGetSpy
beforeEach(async () => {
axiosGetSpy = jest.spyOn(axios, 'get')
.mockImplementationOnce(() => Promise.resolve({
status: 200,
data: [],
}))
wrapper = mountSearchInput()
const inputField = wrapper.find(inputSelector)
await inputField.setValue('orga')
await wrapper.setData({
searchResults: [{
id: 999,
projectId: 1,
}],
openprojectUrl: 'https://openproject.com',
})
await localVue.nextTick()
await wrapper.setProps({
isSmartPicker: true,
})
await localVue.nextTick()
})
afterEach(() => {
axiosGetSpy.mockRestore()
})
it('should emit an action', async () => {
const ncSelectItem = wrapper.find(firstWorkPackageSelector)
await ncSelectItem.trigger('click')
const savedEvent = wrapper.emitted('submit')
expect(savedEvent).toHaveLength(1)
expect(savedEvent[0][0]).toEqual('https://openproject.com/projects/1/work_packages/999')
})

it('should not send a request to link file to workpackage', async () => {
const postSpy = jest.spyOn(axios, 'post')
.mockImplementationOnce(() => Promise.resolve({
status: 200,
}))
const ncSelectItem = wrapper.find(firstWorkPackageSelector)
await ncSelectItem.trigger('click')
expect(postSpy).not.toBeCalled()
postSpy.mockRestore()
})
})
})

function mountSearchInput(fileInfo = {}, linkedWorkPackages = [], data = {}) {
return mount(SearchInput, {
localVue,
Expand Down

0 comments on commit e8c209d

Please sign in to comment.