Skip to content

Commit

Permalink
fix: fix singleton update spec
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Dec 12, 2024
1 parent 439dc3b commit 9f6b2c1
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 43 deletions.
9 changes: 5 additions & 4 deletions gen/templates/spec.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('##__RESOURCE_CLASS__## resource', () => {
})

await clp[resourcePath].create(resData, params, CommonData.options)
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -62,7 +62,7 @@ describe('##__RESOURCE_CLASS__## resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -81,14 +81,15 @@ describe('##__RESOURCE_CLASS__## resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
9 changes: 5 additions & 4 deletions specs/resources/api_credentials.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('ApiCredentials resource', () => {
})

await clp[resourcePath].create(resData, params, CommonData.options)
.then((res: ApiCredential) => expect(res).not.toBeNull())
.then((res: ApiCredential) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -68,7 +68,7 @@ describe('ApiCredentials resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: ApiCredential) => expect(res).not.toBeNull())
.then((res: ApiCredential) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -87,14 +87,15 @@ describe('ApiCredentials resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: ApiCredential) => expect(res).not.toBeNull())
.then((res: ApiCredential) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
9 changes: 5 additions & 4 deletions specs/resources/application_memberships.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('ApplicationMemberships resource', () => {
})

await clp[resourcePath].create(resData, params, CommonData.options)
.then((res: ApplicationMembership) => expect(res).not.toBeNull())
.then((res: ApplicationMembership) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -68,7 +68,7 @@ describe('ApplicationMemberships resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: ApplicationMembership) => expect(res).not.toBeNull())
.then((res: ApplicationMembership) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -87,14 +87,15 @@ describe('ApplicationMemberships resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: ApplicationMembership) => expect(res).not.toBeNull())
.then((res: ApplicationMembership) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
9 changes: 5 additions & 4 deletions specs/resources/memberships.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Memberships resource', () => {
})

await clp[resourcePath].create(resData, params, CommonData.options)
.then((res: Membership) => expect(res).not.toBeNull())
.then((res: Membership) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -68,7 +68,7 @@ describe('Memberships resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: Membership) => expect(res).not.toBeNull())
.then((res: Membership) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -87,14 +87,15 @@ describe('Memberships resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: Membership) => expect(res).not.toBeNull())
.then((res: Membership) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
9 changes: 5 additions & 4 deletions specs/resources/organizations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Organizations resource', () => {
})

await clp[resourcePath].create(resData, params, CommonData.options)
.then((res: Organization) => expect(res).not.toBeNull())
.then((res: Organization) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -65,7 +65,7 @@ describe('Organizations resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: Organization) => expect(res).not.toBeNull())
.then((res: Organization) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -84,14 +84,15 @@ describe('Organizations resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: Organization) => expect(res).not.toBeNull())
.then((res: Organization) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
9 changes: 5 additions & 4 deletions specs/resources/permissions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Permissions resource', () => {
})

await clp[resourcePath].create(resData, params, CommonData.options)
.then((res: Permission) => expect(res).not.toBeNull())
.then((res: Permission) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -70,7 +70,7 @@ describe('Permissions resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: Permission) => expect(res).not.toBeNull())
.then((res: Permission) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -89,14 +89,15 @@ describe('Permissions resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: Permission) => expect(res).not.toBeNull())
.then((res: Permission) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
9 changes: 5 additions & 4 deletions specs/resources/roles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Roles resource', () => {
})

await clp[resourcePath].create(resData, params, CommonData.options)
.then((res: Role) => expect(res).not.toBeNull())
.then((res: Role) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -66,7 +66,7 @@ describe('Roles resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: Role) => expect(res).not.toBeNull())
.then((res: Role) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand All @@ -85,14 +85,15 @@ describe('Roles resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: Role) => expect(res).not.toBeNull())
.then((res: Role) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
5 changes: 3 additions & 2 deletions specs/resources/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ describe('Users resource', () => {
if (request.options.method !== 'GET') {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourcePath, resData.id, currentAccessToken)
const id = clp.isSingleton(resourceType)? undefined : resData.id
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
}
return interceptRequest()
})

await clp[resourcePath].update(resData, params, CommonData.options)
.then((res: User) => expect(res).not.toBeNull())
.then((res: User) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
2 changes: 1 addition & 1 deletion specs/resources/versions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Versions resource', () => {
})

await clp[resourcePath].retrieve(id, params, CommonData.options)
.then((res: Version) => expect(res).not.toBeNull())
.then((res: Version) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => clp.removeInterceptor('request', intId))

Expand Down
24 changes: 12 additions & 12 deletions test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const INTERCEPTOR_CANCEL = 'TEST-INTERCEPTED'
const REQUEST_TIMEOUT = 5550
const REQUEST_OPTIONS: RequestConfig = {
timeout: REQUEST_TIMEOUT,
params: {}
params: { }
} as const

export const TestData = {
Expand Down Expand Up @@ -60,14 +60,14 @@ let currentAccessToken: string
const initClient = async (config: CommerceLayerConfig): Promise<CommerceLayerProvisioningClient> => {

let accessToken: string

if (config.accessToken) accessToken = config.accessToken
else {
const token = await getToken('user')
if (token === null) throw new Error('Unable to get access token')
accessToken = token.accessToken
}


const client = CommerceLayerProvisioning({ accessToken, domain })
currentAccessToken = accessToken

Expand Down Expand Up @@ -124,20 +124,20 @@ const randomValue = (type: string, name?: string): any | Array<any> => {

if (type.startsWith('boolean')) values = booleans
else
if (type.startsWith('integer') || type.startsWith('number')) values = numbers
else
if (type.startsWith('fload') || type.startsWith('decimal')) values = numbers
else
if (type.startsWith('object')) values = objects
else
if (type.startsWith('string')) values = strings
else values = strings
if (type.startsWith('integer') || type.startsWith('number')) values = numbers
else
if (type.startsWith('fload') || type.startsWith('decimal')) values = numbers
else
if (type.startsWith('object')) values = objects
else
if (type.startsWith('string')) values = strings
else values = strings

let value = values[Math.floor(Math.random() * (values.length - 1))]

if (type === 'string') value = `${value}_${Math.floor(Math.random() * 100)}`

if (type.endsWith('[]')) value = [value]
if (type.endsWith('[]')) value = [ value ]

return value

Expand Down Expand Up @@ -168,7 +168,7 @@ const checkCommonData = (data: any, type: string, attributes: any, id?: string)
}

const checkParam = (url: string | URL, name: string, value: string | number | boolean) => {
const params = (url instanceof URL) ? url.searchParams : new URL(url).searchParams
const params = (url instanceof URL)? url.searchParams : new URL(url).searchParams
expect(params.has(name)).toBeTruthy()
expect(params.get(name)).toBe(String(value))
}
Expand Down

0 comments on commit 9f6b2c1

Please sign in to comment.