mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-17 09:00:06 +00:00
test: added tests for folder move function
This commit is contained in:
@@ -1,17 +1,5 @@
|
|||||||
import { SASViyaApiClient } from '../SASViyaApiClient'
|
import { SASViyaApiClient } from '../SASViyaApiClient'
|
||||||
|
|
||||||
const sampleResponse = `{
|
|
||||||
"creationTimeStamp": "2021-01-06T14:09:27.705Z",
|
|
||||||
"modifiedTimeStamp": "2021-01-06T14:46:57.391Z",
|
|
||||||
"createdBy": "dctestuser1",
|
|
||||||
"modifiedBy": "dctestuser1",
|
|
||||||
"id": "00000-00000-00000-00000-00000",
|
|
||||||
"name": "test",
|
|
||||||
"parentFolderUri":"/folders/folders/00000-00000-00000-00000-00000",
|
|
||||||
"type": "folder",
|
|
||||||
"memberCount":"1"
|
|
||||||
}`
|
|
||||||
|
|
||||||
describe('FolderOperations', () => {
|
describe('FolderOperations', () => {
|
||||||
let originalFetch: any
|
let originalFetch: any
|
||||||
|
|
||||||
@@ -26,11 +14,107 @@ describe('FolderOperations', () => {
|
|||||||
originalFetch = (global as any).fetch
|
originalFetch = (global as any).fetch
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {})
|
||||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
|
||||||
Promise.resolve({
|
afterAll(() => {
|
||||||
text: () => Promise.resolve(sampleResponse),
|
;(global as any).fetch = originalFetch
|
||||||
json: () => Promise.resolve(sampleResponse),
|
})
|
||||||
|
|
||||||
|
it('should move and rename folder', async (done) => {
|
||||||
|
mockFetchResponse(false)
|
||||||
|
|
||||||
|
let res: any = await sasViyaApiClient.moveFolder(
|
||||||
|
'/Test/fromFolder/oldName',
|
||||||
|
'/Test/toFolder/newName',
|
||||||
|
'newName',
|
||||||
|
'token'
|
||||||
|
)
|
||||||
|
|
||||||
|
let jsonResponse = JSON.parse(res)
|
||||||
|
|
||||||
|
expect(jsonResponse.name).toEqual('newName')
|
||||||
|
expect(jsonResponse.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should move and keep the name of folder', async (done) => {
|
||||||
|
mockFetchResponse(true)
|
||||||
|
|
||||||
|
let res: any = await sasViyaApiClient.moveFolder(
|
||||||
|
'/Test/fromFolder/oldName',
|
||||||
|
'/Test/toFolder',
|
||||||
|
'toFolder',
|
||||||
|
'token'
|
||||||
|
)
|
||||||
|
|
||||||
|
let jsonResponse = JSON.parse(res)
|
||||||
|
|
||||||
|
expect(jsonResponse.name).toEqual('oldName')
|
||||||
|
expect(jsonResponse.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should only rename folder', async (done) => {
|
||||||
|
mockFetchResponse(false)
|
||||||
|
|
||||||
|
let res: any = await sasViyaApiClient.moveFolder(
|
||||||
|
'/Test/toFolder/oldName',
|
||||||
|
'/Test/toFolder/newName',
|
||||||
|
'newName',
|
||||||
|
'token'
|
||||||
|
)
|
||||||
|
|
||||||
|
let jsonResponse = JSON.parse(res)
|
||||||
|
|
||||||
|
expect(jsonResponse.name).toEqual('newName')
|
||||||
|
expect(jsonResponse.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const mockFetchResponse = (targetFolderExists: boolean) => {
|
||||||
|
;(global as any).fetch = jest
|
||||||
|
.fn()
|
||||||
|
.mockImplementation((url: any, request: any) => {
|
||||||
|
console.log(`[url]`, url)
|
||||||
|
console.log(`[request]`, request)
|
||||||
|
|
||||||
|
if (
|
||||||
|
request.method === 'GET' &&
|
||||||
|
!targetFolderExists &&
|
||||||
|
url.includes('newName')
|
||||||
|
) {
|
||||||
|
return Promise.resolve({
|
||||||
|
text: () => Promise.resolve(undefined),
|
||||||
|
json: () => Promise.resolve(undefined),
|
||||||
|
ok: true,
|
||||||
|
headers: {
|
||||||
|
get: function () {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.method === 'GET' && url.includes('/Test/toFolder')) {
|
||||||
|
return Promise.resolve({
|
||||||
|
text: () => Promise.resolve({ id: url }),
|
||||||
|
json: () => Promise.resolve({ id: url }),
|
||||||
|
ok: true,
|
||||||
|
headers: {
|
||||||
|
get: function () {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve({
|
||||||
|
text: () => Promise.resolve(request.body),
|
||||||
|
json: () => Promise.resolve(request.body),
|
||||||
ok: true,
|
ok: true,
|
||||||
headers: {
|
headers: {
|
||||||
get: function () {
|
get: function () {
|
||||||
@@ -38,22 +122,5 @@ describe('FolderOperations', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
})
|
||||||
})
|
}
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
;(global as any).fetch = originalFetch
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should move folder successfully', async (done) => {
|
|
||||||
let res: any = await sasViyaApiClient.moveFolder(
|
|
||||||
'/Test/test',
|
|
||||||
'/Test/toFolder',
|
|
||||||
'toFolder',
|
|
||||||
'token'
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(JSON.stringify(res)).toEqual(JSON.stringify(sampleResponse))
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user