Skip to content

Commit

Permalink
test: rewrite rest adapter tests - some still failing - sorry
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Sep 11, 2024
1 parent bd7b858 commit f598e33
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 192 deletions.
13 changes: 2 additions & 11 deletions test/unit/adapters/REST/endpoints/app-action-call.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { vi, expect, describe, it } from 'vitest'
import { expect, describe, it } from 'vitest'
import { cloneMock } from '../../../mocks/entities'
import setupRestAdapter from '../helpers/setupRestAdapter'
import contentfulSdkCore from 'contentful-sdk-core'

import {
CreateAppActionCallProps,
wrapAppActionCallResponse,
} from '../../../../../lib/entities/app-action-call'
import { MakeRequest, MakeRequestOptions } from '../../../../../lib/export-types'

vi.mock('contentful-sdk-core', async (importOriginal) => {
const orig = await importOriginal<typeof contentfulSdkCore>()
return {
...orig,
createHttpClient: vi.fn(),
}
})

function setup(promise, mockName, params = {}) {
const entityMock = cloneMock(mockName)
return {
Expand All @@ -25,7 +16,7 @@ function setup(promise, mockName, params = {}) {
}
}

describe('Rest App Action Call', { concurrent: 3 }, () => {
describe('Rest App Action Call', { concurrent: true }, () => {
it('should create a new App Action Call', async () => {
const responseMock = cloneMock('appActionCallResponse')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, test } from 'mocha'
import { vi, expect, describe, test } from 'vitest'
import { cloneMock, assetWithFilesMock } from '../../../mocks/entities'
import { wrapAsset } from '../../../../../lib/entities/asset'
import { expect } from 'chai'
import setupRestAdapter from '../helpers/setupRestAdapter'
import contentfulSdkCore from 'contentful-sdk-core'

function setup(promise, params = {}) {
return {
Expand All @@ -11,6 +11,14 @@ function setup(promise, params = {}) {
}
}

vi.mock('contentful-sdk-core', async (importOriginal) => {
const orig = await importOriginal<typeof contentfulSdkCore>()
return {
...orig,
createHttpClient: vi.fn(),
}
})

describe('Rest Asset', async () => {
test('Asset processing for multiple locales succeeds', async () => {
const responseMock = cloneMock('asset')
Expand Down Expand Up @@ -42,28 +50,28 @@ describe('Rest Asset', async () => {
entityMock.sys.version = 2
const entity = wrapAsset((...args) => adapterMock.makeRequest(...args), entityMock)
return entity.processForAllLocales().then(() => {
expect(httpMock.put.args[0][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id/files/en-US/process',
expect(httpMock.put.mock.calls[0][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id/files/en-US/process',
'en-US locale is sent'
)
expect(httpMock.put.args[1][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id/files/de-DE/process',
expect(httpMock.put.mock.calls[1][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id/files/de-DE/process',
'de-DE locale is sent'
)
expect(httpMock.put.args[0][2].headers['X-Contentful-Version']).equals(
expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals(
2,
'version header is sent for first locale'
)
expect(httpMock.put.args[1][2].headers['X-Contentful-Version']).equals(
expect(httpMock.put.mock.calls[1][2].headers['X-Contentful-Version']).equals(
2,
'version header is sent for second locale'
)
expect(httpMock.get.args[0][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id',
expect(httpMock.get.mock.calls[0][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id',
'asset was checked after processing for first locale'
)
expect(httpMock.get.args[1][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id',
expect(httpMock.get.mock.calls[1][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id',
'asset was checked after processing for second locale'
)
})
Expand All @@ -83,26 +91,22 @@ describe('Rest Asset', async () => {
entityMock.sys.version = 2
const entity = wrapAsset((...args) => adapterMock.makeRequest(...args), entityMock)
return entity.processForLocale('en-US').then(() => {
expect(httpMock.put.args[0][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id/files/en-US/process',
expect(httpMock.put.mock.calls[0][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id/files/en-US/process',
'correct locale is sent'
)
expect(httpMock.put.args[0][2].headers['X-Contentful-Version']).equals(
expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals(
2,
'version header is sent'
)
expect(httpMock.get.args[0][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id',
expect(httpMock.get.mock.calls[0][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id',
'asset was checked after processing'
)
})
})

/*
- Test is failing
- it also causes a memory leak in watch mode
*/
test.skip('Asset processing for one locale fails due to timeout', async () => {
test('Asset processing for one locale fails due to timeout', { timeout: 35_000 }, async () => {
const responseMock = cloneMock('asset')
responseMock.fields = {
file: { 'en-US': { fileName: 'filename.jpg' } }, // url property never sent in response
Expand All @@ -113,7 +117,7 @@ describe('Rest Asset', async () => {
try {
await entity.processForLocale('en-US')
} catch (error) {
expect(httpMock.get.callCount > 1, 'asset is checked multiple times').to.be.ok
expect(httpMock.get.mock.calls.length, 'asset is checked multiple times').toBeGreaterThan(1)
expect(error.name).equals('AssetProcessingTimeout', 'timeout is thrown')
}
})
Expand Down Expand Up @@ -148,61 +152,65 @@ describe('Rest Asset', async () => {
entityMock.sys.version = 2
const entity = wrapAsset((...args) => adapterMock.makeRequest(...args), entityMock)
return entity.processForAllLocales().then(() => {
expect(httpMock.put.args[0][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id/files/en-US/process',
expect(httpMock.put.mock.calls[0][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id/files/en-US/process',
'en-US locale is sent'
)
expect(httpMock.put.args[1][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id/files/de-DE/process',
expect(httpMock.put.mock.calls[1][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id/files/de-DE/process',
'de-DE locale is sent'
)
expect(httpMock.put.args[0][2].headers['X-Contentful-Version']).equals(
expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals(
2,
'version header is sent for first locale'
)
expect(httpMock.put.args[1][2].headers['X-Contentful-Version']).equals(
expect(httpMock.put.mock.calls[1][2].headers['X-Contentful-Version']).equals(
2,
'version header is sent for second locale'
)
expect(httpMock.get.args[0][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id',
expect(httpMock.get.mock.calls[0][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id',
'asset was checked after processing for first locale'
)
expect(httpMock.get.args[1][0]).equals(
'/spaces/space-id/environments/environment-id/assets/id',
expect(httpMock.get.mock.calls[1][0]).equals(
'/spaces/mock-space-id/environments/mock-environment-id/assets/id',
'asset was checked after processing for second locale'
)
})
})

test('Asset processing for multiple locales fails due to timeout', async () => {
const responseMock = cloneMock('asset')
responseMock.fields = {
file: {
'en-US': { fileName: 'filename.jpg' }, // url property never sent
'de-DE': { fileName: 'filename.jpg' }, // url property never sent
},
}
const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({ data: responseMock }))
entityMock.fields = {
file: {
'en-US': {
fileName: 'filename.jpg',
upload: 'http://server/filename.jpg',
test(
'Asset processing for multiple locales fails due to timeout',
{ timeout: 35_000 },
async () => {
const responseMock = cloneMock('asset')
responseMock.fields = {
file: {
'en-US': { fileName: 'filename.jpg' }, // url property never sent
'de-DE': { fileName: 'filename.jpg' }, // url property never sent
},
'de-DE': {
fileName: 'filename.jpg',
upload: 'http://server/filename.jpg',
}
const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({ data: responseMock }))
entityMock.fields = {
file: {
'en-US': {
fileName: 'filename.jpg',
upload: 'http://server/filename.jpg',
},
'de-DE': {
fileName: 'filename.jpg',
upload: 'http://server/filename.jpg',
},
},
},
}
entityMock.sys.version = 2
const entity = wrapAsset((...args) => adapterMock.makeRequest(...args), entityMock)
return entity.processForAllLocales().catch((error) => {
expect(httpMock.get.mock.calls.length, 'asset is checked multiple times').toBeGreaterThan(1)
expect(error.name).equals('AssetProcessingTimeout', 'timeout is thrown')
})
}
entityMock.sys.version = 2
const entity = wrapAsset((...args) => adapterMock.makeRequest(...args), entityMock)
return entity.processForAllLocales().catch((error) => {
expect(httpMock.get.callCount > 1, 'asset is checked multiple times').to.be.ok
expect(error.name).equals('AssetProcessingTimeout', 'timeout is thrown')
})
})
)

test('Asset update with tags works', async () => {
const { adapterMock, httpMock } = setup()
Expand All @@ -215,8 +223,11 @@ describe('Rest Asset', async () => {
}
return entity.update().then((response) => {
expect(response.toPlainObject, 'response is wrapped').to.be.ok
expect(httpMock.put.args[0][1].metadata.tags[0].name).equals('newname', 'metadata is sent')
expect(httpMock.put.args[0][2].headers['X-Contentful-Version']).equals(
expect(httpMock.put.mock.calls[0][1].metadata.tags[0].name).equals(
'newname',
'metadata is sent'
)
expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals(
2,
'version header is sent'
)
Expand All @@ -231,7 +242,7 @@ describe('Rest Asset', async () => {
test('API call createAssetFromFiles', async () => {
const { httpMock, adapterMock } = setup(Promise.resolve({}))

httpMock.post.onFirstCall().returns(
httpMock.post.mockReturnValueOnce(
Promise.resolve({
data: {
sys: {
Expand All @@ -241,7 +252,7 @@ describe('Rest Asset', async () => {
})
)

httpMock.post.onSecondCall().returns(
httpMock.post.mockReturnValueOnce(
Promise.resolve({
data: {
sys: {
Expand All @@ -251,7 +262,7 @@ describe('Rest Asset', async () => {
})
)

httpMock.post.onThirdCall().returns(
httpMock.post.mockReturnValueOnce(
Promise.resolve({
data: assetWithFilesMock,
})
Expand Down Expand Up @@ -280,11 +291,11 @@ describe('Rest Asset', async () => {
},
})
.then(() => {
expect(httpMock.post.args[0][1]).equals(
expect(httpMock.post.mock.calls[0][1]).equals(
'<svg xmlns="http://www.w3.org/2000/svg"><path fill="red" d="M50 50h150v50H50z"/></svg>',
'uploads file #1 to upload endpoint'
)
expect(httpMock.post.args[1][1]).equals(
expect(httpMock.post.mock.calls[1][1]).equals(
'<svg xmlns="http://www.w3.org/2000/svg"><path fill="blue" d="M50 50h150v50H50z"/></svg>',
'uploads file #2 to upload endpoint'
)
Expand Down
Loading

0 comments on commit f598e33

Please sign in to comment.