diff --git a/test/unit/adapters/REST/endpoints/app-action-call.test.ts b/test/unit/adapters/REST/endpoints/app-action-call.test.ts index 57449b2651..9dc693a4e8 100644 --- a/test/unit/adapters/REST/endpoints/app-action-call.test.ts +++ b/test/unit/adapters/REST/endpoints/app-action-call.test.ts @@ -1,7 +1,6 @@ -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, @@ -9,14 +8,6 @@ import { } from '../../../../../lib/entities/app-action-call' import { MakeRequest, MakeRequestOptions } from '../../../../../lib/export-types' -vi.mock('contentful-sdk-core', async (importOriginal) => { - const orig = await importOriginal() - return { - ...orig, - createHttpClient: vi.fn(), - } -}) - function setup(promise, mockName, params = {}) { const entityMock = cloneMock(mockName) return { @@ -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') diff --git a/test/unit/adapters/REST/endpoints/asset-test.js b/test/unit/adapters/REST/endpoints/asset.test.ts similarity index 62% rename from test/unit/adapters/REST/endpoints/asset-test.js rename to test/unit/adapters/REST/endpoints/asset.test.ts index 66b2739c46..f9622edeb6 100644 --- a/test/unit/adapters/REST/endpoints/asset-test.js +++ b/test/unit/adapters/REST/endpoints/asset.test.ts @@ -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 { @@ -11,6 +11,14 @@ function setup(promise, params = {}) { } } +vi.mock('contentful-sdk-core', async (importOriginal) => { + const orig = await importOriginal() + return { + ...orig, + createHttpClient: vi.fn(), + } +}) + describe('Rest Asset', async () => { test('Asset processing for multiple locales succeeds', async () => { const responseMock = cloneMock('asset') @@ -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' ) }) @@ -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 @@ -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') } }) @@ -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() @@ -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' ) @@ -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: { @@ -241,7 +252,7 @@ describe('Rest Asset', async () => { }) ) - httpMock.post.onSecondCall().returns( + httpMock.post.mockReturnValueOnce( Promise.resolve({ data: { sys: { @@ -251,7 +262,7 @@ describe('Rest Asset', async () => { }) ) - httpMock.post.onThirdCall().returns( + httpMock.post.mockReturnValueOnce( Promise.resolve({ data: assetWithFilesMock, }) @@ -280,11 +291,11 @@ describe('Rest Asset', async () => { }, }) .then(() => { - expect(httpMock.post.args[0][1]).equals( + expect(httpMock.post.mock.calls[0][1]).equals( '', 'uploads file #1 to upload endpoint' ) - expect(httpMock.post.args[1][1]).equals( + expect(httpMock.post.mock.calls[1][1]).equals( '', 'uploads file #2 to upload endpoint' ) diff --git a/test/unit/adapters/REST/endpoints/concept-scheme-test.ts b/test/unit/adapters/REST/endpoints/concept-scheme.test.ts similarity index 75% rename from test/unit/adapters/REST/endpoints/concept-scheme-test.ts rename to test/unit/adapters/REST/endpoints/concept-scheme.test.ts index dc10aa06a2..985f350f70 100644 --- a/test/unit/adapters/REST/endpoints/concept-scheme-test.ts +++ b/test/unit/adapters/REST/endpoints/concept-scheme.test.ts @@ -1,6 +1,6 @@ -import { describe, test } from 'mocha' +import { expect, describe, test } from 'vitest' + import { cloneMock } from '../../../mocks/entities' -import { expect } from 'chai' import setupRestAdapter from '../helpers/setupRestAdapter' function setup(promise: Promise, params = {}, type = 'conceptScheme') { @@ -13,12 +13,13 @@ function setup(promise: Promise, params = {}, type = 'conceptScheme') { describe('ConceptScheme', () => { test('get', async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: entityMock })) + httpMock.get.mockReturnValue(Promise.resolve({ data: entityMock })) return adapterMock .makeRequest({ entityType: 'ConceptScheme', action: 'get', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptSchemeId: 'concept-scheme-id', @@ -26,7 +27,7 @@ describe('ConceptScheme', () => { }) .then((r) => { expect(r).to.eql(entityMock) - expect(httpMock.get.args[0][0]).to.eql( + expect(httpMock.get.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concept-schemes/concept-scheme-id' ) }) @@ -34,30 +35,32 @@ describe('ConceptScheme', () => { test('getTotal', async () => { const { httpMock, adapterMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: { total: 1 } })) + httpMock.get.mockReturnValue(Promise.resolve({ data: { total: 1 } })) return adapterMock .makeRequest({ entityType: 'ConceptScheme', action: 'getTotal', + userAgent: 'mocked', params: { organizationId: 'organization-id', }, }) .then(() => { - expect(httpMock.get.args[0][0]).to.eql( + expect(httpMock.get.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concept-schemes/total' ) }) }) test('create', async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.post.returns(Promise.resolve({ data: entityMock })) + httpMock.post.mockReturnValue(Promise.resolve({ data: entityMock })) return adapterMock .makeRequest({ entityType: 'ConceptScheme', action: 'create', + userAgent: 'mocked', params: { organizationId: 'organization-id', }, @@ -65,7 +68,7 @@ describe('ConceptScheme', () => { }) .then((r) => { expect(r).to.eql(entityMock) - expect(httpMock.post.args[0][0]).to.eql( + expect(httpMock.post.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concept-schemes' ) }) @@ -73,19 +76,20 @@ describe('ConceptScheme', () => { test('update', async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.patch.returns(Promise.resolve({ data: entityMock })) + httpMock.patch.mockReturnValue(Promise.resolve({ data: entityMock })) return adapterMock .makeRequest({ entityType: 'ConceptScheme', action: 'update', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptSchemeId: 'concept-scheme-id', }, }) .then(() => { - expect(httpMock.patch.args[0][0]).to.eql( + expect(httpMock.patch.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concept-schemes/concept-scheme-id' ) }) @@ -93,12 +97,13 @@ describe('ConceptScheme', () => { test('delete', async () => { const { httpMock, adapterMock } = setup(Promise.resolve({})) - httpMock.delete.returns(Promise.resolve({ data: {} })) + httpMock.delete.mockReturnValue(Promise.resolve({ data: {} })) return adapterMock .makeRequest({ entityType: 'ConceptScheme', action: 'delete', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptSchemeId: 'concept-scheme-id', @@ -106,10 +111,10 @@ describe('ConceptScheme', () => { }, }) .then(() => { - expect(httpMock.delete.args[0][0]).to.eql( + expect(httpMock.delete.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concept-schemes/concept-scheme-id' ) - expect(httpMock.delete.args[0][1].headers).to.eql({ 'X-Contentful-Version': 1 }) + expect(httpMock.delete.mock.calls[0][1].headers).to.eql({ 'X-Contentful-Version': 1 }) }) }) @@ -135,12 +140,13 @@ describe('ConceptScheme', () => { configs.forEach(({ expected, params, name }) => { test(name, async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: { items: [entityMock] } })) + httpMock.get.mockReturnValue(Promise.resolve({ data: { items: [entityMock] } })) return adapterMock .makeRequest({ entityType: 'ConceptScheme', action: 'getMany', + userAgent: 'mocked', params: { organizationId: 'organization-id', ...params, @@ -148,8 +154,8 @@ describe('ConceptScheme', () => { }) .then((r) => { expect(r).to.eql({ items: [entityMock] }) - expect(httpMock.get.args[0][0]).to.eql(expected.url) - expect(httpMock.get.args[0][1].params).to.eql(expected.params) + expect(httpMock.get.mock.calls[0][0]).to.eql(expected.url) + expect(httpMock.get.mock.calls[0][1].params).to.eql(expected.params) }) }) }) diff --git a/test/unit/adapters/REST/endpoints/concept-test.ts b/test/unit/adapters/REST/endpoints/concept.test.ts similarity index 75% rename from test/unit/adapters/REST/endpoints/concept-test.ts rename to test/unit/adapters/REST/endpoints/concept.test.ts index 199f7bcd9c..68020117bd 100644 --- a/test/unit/adapters/REST/endpoints/concept-test.ts +++ b/test/unit/adapters/REST/endpoints/concept.test.ts @@ -1,6 +1,5 @@ -import { describe, test } from 'mocha' +import { expect, describe, test } from 'vitest' import { cloneMock } from '../../../mocks/entities' -import { expect } from 'chai' import setupRestAdapter from '../helpers/setupRestAdapter' function setup(promise, params = {}, type = 'concept') { @@ -14,12 +13,13 @@ describe('Concept', () => { test('get', async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: entityMock })) + httpMock.get.mockReturnValue(Promise.resolve({ data: entityMock })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'get', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptId: 'concept-id', @@ -27,7 +27,7 @@ describe('Concept', () => { }) .then((r) => { expect(r).to.eql(entityMock) - expect(httpMock.get.args[0][0]).to.eql( + expect(httpMock.get.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concepts/concept-id' ) }) @@ -62,12 +62,13 @@ describe('Concept', () => { configs.forEach(({ expected, params, name }) => { test(name, async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: { items: [entityMock] } })) + httpMock.get.mockReturnValue(Promise.resolve({ data: { items: [entityMock] } })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'getMany', + userAgent: 'mocked', params: { organizationId: 'organization-id', ...params, @@ -75,8 +76,8 @@ describe('Concept', () => { }) .then((r) => { expect(r).to.eql({ items: [entityMock] }) - expect(httpMock.get.args[0][0]).to.eql(expected.url) - expect(httpMock.get.args[0][1].params).to.eql(expected.params) + expect(httpMock.get.mock.calls[0][0]).to.eql(expected.url) + expect(httpMock.get.mock.calls[0][1].params).to.eql(expected.params) }) }) }) @@ -84,18 +85,19 @@ describe('Concept', () => { test('getTotal', async () => { const { httpMock, adapterMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: { total: 1 } })) + httpMock.get.mockReturnValue(Promise.resolve({ data: { total: 1 } })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'getTotal', + userAgent: 'mocked', params: { organizationId: 'organization-id', }, }) .then(() => { - expect(httpMock.get.args[0][0]).to.eql( + expect(httpMock.get.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concepts/total' ) }) @@ -103,12 +105,13 @@ describe('Concept', () => { test('create', async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.post.returns(Promise.resolve({ data: entityMock })) + httpMock.post.mockReturnValue(Promise.resolve({ data: entityMock })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'create', + userAgent: 'mocked', params: { organizationId: 'organization-id', }, @@ -116,25 +119,28 @@ describe('Concept', () => { }) .then((r) => { expect(r).to.eql(entityMock) - expect(httpMock.post.args[0][0]).to.eql('/organizations/organization-id/taxonomy/concepts') + expect(httpMock.post.mock.calls[0][0]).to.eql( + '/organizations/organization-id/taxonomy/concepts' + ) }) }) test('update', async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.patch.returns(Promise.resolve({ data: entityMock })) + httpMock.patch.mockReturnValue(Promise.resolve({ data: entityMock })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'update', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptId: 'concept-id', }, }) .then(() => { - expect(httpMock.patch.args[0][0]).to.eql( + expect(httpMock.patch.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concepts/concept-id' ) }) @@ -142,19 +148,20 @@ describe('Concept', () => { test('delete', async () => { const { httpMock, adapterMock } = setup(Promise.resolve({})) - httpMock.delete.returns(Promise.resolve({ data: {} })) + httpMock.delete.mockReturnValue(Promise.resolve({ data: {} })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'delete', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptId: 'concept-id', }, }) .then(() => { - expect(httpMock.delete.args[0][0]).to.eql( + expect(httpMock.delete.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concepts/concept-id' ) }) @@ -162,19 +169,20 @@ describe('Concept', () => { test('getDescendants', async () => { const { httpMock, adapterMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: {} })) + httpMock.get.mockReturnValue(Promise.resolve({ data: {} })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'getDescendants', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptId: 'concept-id', }, }) .then(() => { - expect(httpMock.get.args[0][0]).to.eql( + expect(httpMock.get.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concepts/concept-id/descendants' ) }) @@ -183,19 +191,20 @@ describe('Concept', () => { test('getAncestors', async () => { const { httpMock, adapterMock } = setup(Promise.resolve({})) - httpMock.get.returns(Promise.resolve({ data: {} })) + httpMock.get.mockReturnValue(Promise.resolve({ data: {} })) return adapterMock .makeRequest({ entityType: 'Concept', action: 'getAncestors', + userAgent: 'mocked', params: { organizationId: 'organization-id', conceptId: 'concept-id', }, }) .then(() => { - expect(httpMock.get.args[0][0]).to.eql( + expect(httpMock.get.mock.calls[0][0]).to.eql( '/organizations/organization-id/taxonomy/concepts/concept-id/ancestors' ) }) diff --git a/test/unit/adapters/REST/endpoints/entry-test.js b/test/unit/adapters/REST/endpoints/entry.test.ts similarity index 66% rename from test/unit/adapters/REST/endpoints/entry-test.js rename to test/unit/adapters/REST/endpoints/entry.test.ts index 61372f1564..99503dcb8e 100644 --- a/test/unit/adapters/REST/endpoints/entry-test.js +++ b/test/unit/adapters/REST/endpoints/entry.test.ts @@ -1,7 +1,7 @@ -import { describe, test } from 'mocha' +import { describe, test, expect } from 'vitest' import { cloneMock } from '../../../mocks/entities' import { wrapEntry } from '../../../../../lib/entities/entry' -import { expect } from 'chai' + import setupRestAdapter from '../helpers/setupRestAdapter' function setup(promise, params = {}) { @@ -13,18 +13,23 @@ function setup(promise, params = {}) { describe('Rest Entry', () => { test('Entry update with tags works', async () => { - const { httpMock, adapterMock } = setup() const entityMock = cloneMock('entryWithTags') entityMock.sys.version = 2 + + const { httpMock, adapterMock } = setup(Promise.resolve({ data: entityMock })) + const entity = wrapEntry((...args) => adapterMock.makeRequest(...args), entityMock) - entity.metadata.tags[0] = { - name: 'newname', - sys: entityMock.metadata.tags[0].sys, - } + + entity.metadata.tags = entityMock.metadata.tags + entity.metadata.tags[0].sys.id = 'changed-link-target' + 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].sys.id).equals( + 'changed-link-target', + 'metadata is sent' + ) + expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals( 2, 'version header is sent' ) @@ -39,7 +44,7 @@ describe('Rest Entry', () => { test('API call createEntryWithId', async () => { const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) - httpMock.put.returns(Promise.resolve({ data: entityMock })) + httpMock.put.mockReturnValue(Promise.resolve({ data: entityMock })) return adapterMock .makeRequest({ @@ -55,12 +60,12 @@ describe('Rest Entry', () => { }) .then((r) => { expect(r).to.eql(entityMock) - expect(httpMock.put.args[0][0]).to.eql( + expect(httpMock.put.mock.calls[0][0]).to.eql( '/spaces/id/environments/id/entries/entryId', 'entry id is sent' ) - expect(httpMock.put.args[0][1]).to.eql(entityMock, 'data is sent') - expect(httpMock.put.args[0][2].headers['X-Contentful-Content-Type']).to.eql( + expect(httpMock.put.mock.calls[0][1]).to.eql(entityMock, 'data is sent') + expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Content-Type']).to.eql( 'contentTypeId', 'content type is specified' ) diff --git a/test/unit/adapters/REST/endpoints/environment-template-test.ts b/test/unit/adapters/REST/endpoints/environment-template.test.ts similarity index 80% rename from test/unit/adapters/REST/endpoints/environment-template-test.ts rename to test/unit/adapters/REST/endpoints/environment-template.test.ts index a27f7af3cb..403d58d5d2 100644 --- a/test/unit/adapters/REST/endpoints/environment-template-test.ts +++ b/test/unit/adapters/REST/endpoints/environment-template.test.ts @@ -1,6 +1,5 @@ -import { describe } from 'mocha' +import { describe, expect } from 'vitest' import { cloneMock } from '../../../mocks/entities' -import { expect } from 'chai' import setupRestAdapter from '../helpers/setupRestAdapter' describe('Environment Template', async () => { @@ -17,7 +16,7 @@ describe('Environment Template', async () => { actions.forEach((action) => { it(`propagates custom headers for ${action.name} request`, async () => { - const { adapterMock, httpMock } = setupRestAdapter() + const { adapterMock, httpMock } = setupRestAdapter(Promise.resolve({})) const entityMock = cloneMock(mockName) @@ -30,7 +29,7 @@ describe('Environment Template', async () => { userAgent: 'mockedAgent', }) - expect(httpMock[action.httpMethod].args[0][2].headers['X-Test']).equals('test header') + expect(httpMock[action.httpMethod].mock.calls[0][2].headers['X-Test']).equals('test header') }) }) }) diff --git a/test/unit/adapters/REST/endpoints/extension-test.js b/test/unit/adapters/REST/endpoints/extension.test.ts similarity index 82% rename from test/unit/adapters/REST/endpoints/extension-test.js rename to test/unit/adapters/REST/endpoints/extension.test.ts index 16759e3b7c..8b029e988f 100644 --- a/test/unit/adapters/REST/endpoints/extension-test.js +++ b/test/unit/adapters/REST/endpoints/extension.test.ts @@ -1,4 +1,4 @@ -import { describe } from 'mocha' +import { describe } from 'vitest' import { reusableEntityUpdateTest } from '../reusable-tests/update' describe('Rest Extension', () => { diff --git a/test/unit/adapters/REST/endpoints/organization-membership-test.js b/test/unit/adapters/REST/endpoints/organization-membership.test.ts similarity index 83% rename from test/unit/adapters/REST/endpoints/organization-membership-test.js rename to test/unit/adapters/REST/endpoints/organization-membership.test.ts index ca27c49ddd..2ab1ce6f97 100644 --- a/test/unit/adapters/REST/endpoints/organization-membership-test.js +++ b/test/unit/adapters/REST/endpoints/organization-membership.test.ts @@ -1,5 +1,4 @@ -import { expect } from 'chai' -import { describe, test } from 'mocha' +import { describe, test, expect } from 'vitest' import { cloneMock } from '../../../mocks/entities' import { wrapOrganizationMembership } from '../../../../../lib/entities/organization-membership' import setupRestAdapter from '../helpers/setupRestAdapter' @@ -13,7 +12,7 @@ function setup(promise, params = {}) { describe('Rest Organization Membership', () => { test('OrganizationMembership delete', async () => { - const { httpMock, entityMock, adapterMock } = setup() + const { httpMock, entityMock, adapterMock } = setup(Promise.resolve({})) entityMock.sys.version = 2 const entity = wrapOrganizationMembership( (...args) => adapterMock.makeRequest(...args), @@ -21,7 +20,7 @@ describe('Rest Organization Membership', () => { 'org-id' ) return entity.delete().then((response) => { - expect(httpMock.delete.args[0][0]).equals( + expect(httpMock.delete.mock.calls[0][0]).equals( `/organizations/org-id/organization_memberships/${entityMock.sys.id}`, 'url is correct' ) diff --git a/test/unit/adapters/REST/endpoints/team-membership-test.js b/test/unit/adapters/REST/endpoints/team-membership.test.ts similarity index 61% rename from test/unit/adapters/REST/endpoints/team-membership-test.js rename to test/unit/adapters/REST/endpoints/team-membership.test.ts index 1b2975b1a9..b7cdfbe42d 100644 --- a/test/unit/adapters/REST/endpoints/team-membership-test.js +++ b/test/unit/adapters/REST/endpoints/team-membership.test.ts @@ -1,5 +1,4 @@ -import { expect } from 'chai' -import { describe, test } from 'mocha' +import { expect, describe, test } from 'vitest' import { cloneMock } from '../../../mocks/entities' import { wrapTeamMembership } from '../../../../../lib/entities/team-membership' import setupRestAdapter from '../helpers/setupRestAdapter' @@ -13,18 +12,18 @@ function setup(promise, params = {}) { describe('Rest Team Membership', () => { test('TeamMembership update', async () => { - const { httpMock, entityMock, adapterMock } = setup() + const { httpMock, entityMock, adapterMock } = setup(Promise.resolve({})) entityMock.sys.version = 2 - entityMock.sys.team = { sys: { id: 'team1' } } + entityMock.sys.team = { sys: { id: 'team1', linkType: 'Team', type: 'Link' } } const entity = wrapTeamMembership((...args) => adapterMock.makeRequest(...args), entityMock) entity.admin = true return entity.update().then((response) => { expect(response.toPlainObject, 'response is wrapped').to.be.ok - expect(httpMock.put.args[0][0]).equals( - `/organizations/org-id/teams/team1/team_memberships/${entityMock.sys.id}`, + expect(httpMock.put.mock.calls[0][0]).equals( + `/organizations/mock-organization-id/teams/team1/team_memberships/${entityMock.sys.id}`, 'url is correct' ) - 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' ) @@ -37,13 +36,13 @@ describe('Rest Team Membership', () => { }) test('TeamMembership delete', async () => { - const { httpMock, entityMock, adapterMock } = setup() + const { httpMock, entityMock, adapterMock } = setup(Promise.resolve({})) entityMock.sys.version = 2 - entityMock.sys.team = { sys: { id: 'team1' } } + entityMock.sys.team = { sys: { id: 'team1', linkType: 'Team', type: 'Link' } } const entity = wrapTeamMembership((...args) => adapterMock.makeRequest(...args), entityMock) return entity.delete().then((response) => { - expect(httpMock.delete.args[0][0]).equals( - `/organizations/org-id/teams/team1/team_memberships/${entityMock.sys.id}`, + expect(httpMock.delete.mock.calls[0][0]).equals( + `/organizations/mock-organization-id/teams/team1/team_memberships/${entityMock.sys.id}`, 'url is correct' ) return { diff --git a/test/unit/adapters/REST/endpoints/team-test.js b/test/unit/adapters/REST/endpoints/team.test.ts similarity index 68% rename from test/unit/adapters/REST/endpoints/team-test.js rename to test/unit/adapters/REST/endpoints/team.test.ts index 42ed4af640..07ca6a95fa 100644 --- a/test/unit/adapters/REST/endpoints/team-test.js +++ b/test/unit/adapters/REST/endpoints/team.test.ts @@ -1,5 +1,4 @@ -import { expect } from 'chai' -import { describe, test } from 'mocha' +import { expect, describe, test } from 'vitest' import { cloneMock } from '../../../mocks/entities' import { wrapTeam } from '../../../../../lib/entities/team' import setupRestAdapter from '../helpers/setupRestAdapter' @@ -13,17 +12,17 @@ function setup(promise, params = {}) { describe('Rest Team', () => { test('Team update', async () => { - const { httpMock, adapterMock, entityMock } = setup() + const { httpMock, adapterMock, entityMock } = setup(Promise.resolve({})) entityMock.sys.version = 2 const entity = wrapTeam((...args) => adapterMock.makeRequest(...args), entityMock) entity.description = 'new description' return entity.update().then((response) => { expect(response.toPlainObject, 'response is wrapped').to.be.ok - expect(httpMock.put.args[0][0]).equals( - `/organizations/org-id/teams/${entityMock.sys.id}`, + expect(httpMock.put.mock.calls[0][0]).equals( + `/organizations/mock-organization-id/teams/${entityMock.sys.id}`, 'url is correct' ) - 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' ) @@ -36,12 +35,12 @@ describe('Rest Team', () => { }) test('Team delete', async () => { - const { httpMock, entityMock, adapterMock } = setup() + const { httpMock, entityMock, adapterMock } = setup(Promise.resolve({})) entityMock.sys.version = 2 const entity = wrapTeam((...args) => adapterMock.makeRequest(...args), entityMock) return entity.delete().then((response) => { - expect(httpMock.delete.args[0][0]).equals( - `/organizations/org-id/teams/${entityMock.sys.id}`, + expect(httpMock.delete.mock.calls[0][0]).equals( + `/organizations/mock-organization-id/teams/${entityMock.sys.id}`, 'url is correct' ) return { diff --git a/test/unit/adapters/REST/endpoints/ui-config-test.js b/test/unit/adapters/REST/endpoints/ui-config.test.ts similarity index 69% rename from test/unit/adapters/REST/endpoints/ui-config-test.js rename to test/unit/adapters/REST/endpoints/ui-config.test.ts index 88d6c0dd45..86cf934bad 100644 --- a/test/unit/adapters/REST/endpoints/ui-config-test.js +++ b/test/unit/adapters/REST/endpoints/ui-config.test.ts @@ -1,5 +1,4 @@ -import { expect } from 'chai' -import { describe, test } from 'mocha' +import { describe, test, expect } from 'vitest' import { wrapUIConfig } from '../../../../../lib/entities/ui-config' import { cloneMock } from '../../../mocks/entities' import setupRestAdapter from '../helpers/setupRestAdapter' @@ -13,16 +12,16 @@ function setup(promise, params = {}) { describe('Rest UIConfig', () => { test('UIConfig update works', async () => { - const { httpMock, adapterMock } = setup() + const { httpMock, adapterMock } = setup(Promise.resolve({})) const entityMock = cloneMock('uiConfig') entityMock.sys.version = 2 const entity = wrapUIConfig((...args) => adapterMock.makeRequest(...args), entityMock) - entity.entryListViews[0] = 'view' + entity.entryListViews = [{ id: 'view', title: 'View', views: [] }] return entity.update().then((response) => { expect(response.toPlainObject, 'response is wrapped').to.be.ok - expect(httpMock.put.args[0][1].entryListViews[0]).equals('view', 'metadata is sent') - expect(httpMock.put.args[0][2].headers['X-Contentful-Version']).equals( + expect(httpMock.put.mock.calls[0][1].entryListViews[0].id).equals('view', 'metadata is sent') + expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals( 2, 'version header is sent' ) diff --git a/test/unit/adapters/REST/endpoints/upload-test.js b/test/unit/adapters/REST/endpoints/upload.test.ts similarity index 74% rename from test/unit/adapters/REST/endpoints/upload-test.js rename to test/unit/adapters/REST/endpoints/upload.test.ts index 309018b090..194e758bf9 100644 --- a/test/unit/adapters/REST/endpoints/upload-test.js +++ b/test/unit/adapters/REST/endpoints/upload.test.ts @@ -1,6 +1,6 @@ -import { describe, test } from 'mocha' +import { describe, test, expect } from 'vitest' import { cloneMock } from '../../../mocks/entities' -import { expect } from 'chai' + import setupRestAdapter from '../helpers/setupRestAdapter' function setup(promise, params = {}) { @@ -29,9 +29,11 @@ describe('Rest Upload', async () => { }, }) .then(() => { - expect(httpMock.post.args[0][0]).equals('/spaces/id/environments/envId/uploads') - expect(httpMock.post.args[0][2].headers['Content-Type']).equals('application/octet-stream') - expect(httpMock.post.args[0][1]).equals( + expect(httpMock.post.mock.calls[0][0]).equals('/spaces/id/environments/envId/uploads') + expect(httpMock.post.mock.calls[0][2].headers['Content-Type']).equals( + 'application/octet-stream' + ) + expect(httpMock.post.mock.calls[0][1]).equals( '', 'uploads file to upload endpoint' ) @@ -55,9 +57,11 @@ describe('Rest Upload', async () => { }, }) .then(() => { - expect(httpMock.post.args[0][0]).equals('/spaces/id/uploads') - expect(httpMock.post.args[0][2].headers['Content-Type']).equals('application/octet-stream') - expect(httpMock.post.args[0][1]).equals( + expect(httpMock.post.mock.calls[0][0]).equals('/spaces/id/uploads') + expect(httpMock.post.mock.calls[0][2].headers['Content-Type']).equals( + 'application/octet-stream' + ) + expect(httpMock.post.mock.calls[0][1]).equals( '', 'uploads file to upload endpoint' ) @@ -79,8 +83,10 @@ describe('Rest Upload', async () => { }, }) .then(() => { - expect(httpMock.post.args[0][2].headers['Content-Type']).equals('application/octet-stream') - expect(httpMock.post.args[0][1]).equals( + expect(httpMock.post.mock.calls[0][2].headers['Content-Type']).equals( + 'application/octet-stream' + ) + expect(httpMock.post.mock.calls[0][1]).equals( '', 'uploads file to upload endpoint' ) diff --git a/test/unit/adapters/REST/endpoints/user-ui-config-test.js b/test/unit/adapters/REST/endpoints/user-ui-config.test.ts similarity index 69% rename from test/unit/adapters/REST/endpoints/user-ui-config-test.js rename to test/unit/adapters/REST/endpoints/user-ui-config.test.ts index 2b9e621759..dbe3c96a55 100644 --- a/test/unit/adapters/REST/endpoints/user-ui-config-test.js +++ b/test/unit/adapters/REST/endpoints/user-ui-config.test.ts @@ -1,5 +1,4 @@ -import { expect } from 'chai' -import { describe, test } from 'mocha' +import { describe, test, expect } from 'vitest' import { wrapUIConfig } from '../../../../../lib/entities/ui-config' import { cloneMock } from '../../../mocks/entities' import setupRestAdapter from '../helpers/setupRestAdapter' @@ -13,16 +12,16 @@ function setup(promise, params = {}) { describe('Rest UserUIConfig', () => { test('UIConfig update works', async () => { - const { httpMock, adapterMock } = setup() + const { httpMock, adapterMock } = setup(Promise.resolve({})) const entityMock = cloneMock('userUIConfig') entityMock.sys.version = 2 const entity = wrapUIConfig((...args) => adapterMock.makeRequest(...args), entityMock) - entity.entryListViews[0] = 'view' + entity.entryListViews = [{ id: 'view', title: 'View', views: [] }] return entity.update().then((response) => { expect(response.toPlainObject, 'response is wrapped').to.be.ok - expect(httpMock.put.args[0][1].entryListViews[0]).equals('view', 'metadata is sent') - expect(httpMock.put.args[0][2].headers['X-Contentful-Version']).equals( + expect(httpMock.put.mock.calls[0][1].entryListViews[0]).equals('view', 'metadata is sent') + expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals( 2, 'version header is sent' ) diff --git a/test/unit/adapters/REST/endpoints/utils-test.js b/test/unit/adapters/REST/endpoints/utils.test.ts similarity index 90% rename from test/unit/adapters/REST/endpoints/utils-test.js rename to test/unit/adapters/REST/endpoints/utils.test.ts index 9726c9cb55..87208b739b 100644 --- a/test/unit/adapters/REST/endpoints/utils-test.js +++ b/test/unit/adapters/REST/endpoints/utils.test.ts @@ -1,5 +1,4 @@ -import { expect } from 'chai' -import { describe, it } from 'mocha' +import { describe, it, expect } from 'vitest' import { normalizeSpaceId } from '../../../../../lib/adapters/REST/endpoints/utils' describe('normalizeSpaceId', () => { diff --git a/test/unit/adapters/REST/endpoints/workflow-definition-test.ts b/test/unit/adapters/REST/endpoints/workflow-definition.test.ts similarity index 86% rename from test/unit/adapters/REST/endpoints/workflow-definition-test.ts rename to test/unit/adapters/REST/endpoints/workflow-definition.test.ts index 10f1c15436..432a9d941d 100644 --- a/test/unit/adapters/REST/endpoints/workflow-definition-test.ts +++ b/test/unit/adapters/REST/endpoints/workflow-definition.test.ts @@ -1,5 +1,4 @@ -import { expect } from 'chai' -import { describe, test } from 'mocha' +import { describe, test, expect } from 'vitest' import { cloneMock } from '../../../mocks/entities' import setupRestAdapter from '../helpers/setupRestAdapter' import { @@ -28,11 +27,11 @@ describe('Rest Workflow Definition', () => { entity.description = 'new description' return entity.update().then((response) => { expect(response.toPlainObject, 'response is wrapped').to.be.ok - expect(httpMock.put.args[0][0]).equals( + expect(httpMock.put.mock.calls[0][0]).equals( `/spaces/${entityMock.sys.space.sys.id}/environments/${entityMock.sys.environment.sys.id}/workflow_definitions/${entityMock.sys.id}`, 'url is correct' ) - 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' ) @@ -52,11 +51,11 @@ describe('Rest Workflow Definition', () => { entityMock ) return entity.delete().then(() => { - expect(httpMock.delete.args[0][0]).equals( + expect(httpMock.delete.mock.calls[0][0]).equals( `/spaces/${entityMock.sys.space.sys.id}/environments/${entityMock.sys.environment.sys.id}/workflow_definitions/${entityMock.sys.id}`, 'url is correct' ) - expect(httpMock.delete.args[0][1].headers['X-Contentful-Version']).equals( + expect(httpMock.delete.mock.calls[0][1].headers['X-Contentful-Version']).equals( 2, 'version header is sent' ) diff --git a/test/unit/adapters/REST/helpers/setupRestAdapter.ts b/test/unit/adapters/REST/helpers/setupRestAdapter.ts index 3b8bebbecf..acb5054068 100644 --- a/test/unit/adapters/REST/helpers/setupRestAdapter.ts +++ b/test/unit/adapters/REST/helpers/setupRestAdapter.ts @@ -1,9 +1,18 @@ -import { MockedFunction } from 'vitest' +import { vi, MockedFunction } from 'vitest' import { RestAdapter } from '../../../../../lib/adapters/REST/rest-adapter' import setupHttpMock from '../../../mocks/http' +import contentfulSdkCore from 'contentful-sdk-core' import { AxiosInstance, createHttpClient } from 'contentful-sdk-core' +vi.mock('contentful-sdk-core', async (importOriginal) => { + const orig = await importOriginal() + return { + ...orig, + createHttpClient: vi.fn(), + } +}) + const createHttpClientMock = >(createHttpClient) export default function setupRestAdapter(httpPromise, params = {}) { diff --git a/test/unit/adapters/REST/reusable-tests/update.js b/test/unit/adapters/REST/reusable-tests/update.ts similarity index 52% rename from test/unit/adapters/REST/reusable-tests/update.js rename to test/unit/adapters/REST/reusable-tests/update.ts index 96a7afd9cd..a79a935070 100644 --- a/test/unit/adapters/REST/reusable-tests/update.js +++ b/test/unit/adapters/REST/reusable-tests/update.ts @@ -1,16 +1,15 @@ import setupRestAdapter from '../helpers/setupRestAdapter' import { cloneMock } from '../../../mocks/entities' -import { expect } from 'chai' -import { it } from 'mocha' +import { expect, it } from 'vitest' export function reusableEntityUpdateTest(entityType, mockName) { it(`emits valid update request`, async () => { - const { adapterMock, httpMock } = setupRestAdapter() - const entityMock = cloneMock(mockName) entityMock.name = 'updated name' entityMock.sys.version = 2 + const { adapterMock, httpMock } = setupRestAdapter(Promise.resolve({ data: entityMock })) + await adapterMock.makeRequest({ entityType, action: 'update', @@ -19,12 +18,15 @@ export function reusableEntityUpdateTest(entityType, mockName) { headers: { 'X-Test': 'test header' }, }) - expect(httpMock.put.args[0][1].name).equals('updated name', 'data is sent') - expect(httpMock.put.args[0][1].sys).equals(undefined, 'sys is removed') - expect(httpMock.put.args[0][2].headers['X-Contentful-Version']).equals( + expect(httpMock.put.mock.calls[0][1].name).equals('updated name', 'data is sent') + expect(httpMock.put.mock.calls[0][1].sys).equals(undefined, 'sys is removed') + expect(httpMock.put.mock.calls[0][2].headers['X-Contentful-Version']).equals( 2, 'version header is sent' ) - expect(httpMock.put.args[0][2].headers['X-Test']).equals('test header', 'custom header is set') + expect(httpMock.put.mock.calls[0][2].headers['X-Test']).equals( + 'test header', + 'custom header is set' + ) }) }