-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #471 from Fekide/470-bug-missing-query-handler-in-…
…unit-tests 470 bug missing query handler in unit tests
- Loading branch information
Showing
7 changed files
with
375 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
plugin/server/services/__tests__/untranslated-service.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict' | ||
|
||
const service = require('../untranslated') | ||
|
||
describe('untranslated service', () => { | ||
describe('getUntranslatedEntity', () => { | ||
it('throws Error if Metadata is not found', () => { | ||
const strapi = { | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => null), | ||
}, | ||
}, | ||
} | ||
|
||
const untranslatedService = service({ strapi }) | ||
|
||
return expect(async () => | ||
untranslatedService.getUntranslatedEntity({ uid: 'uid' }, {}) | ||
).rejects.toThrow('Content Type does not exist') | ||
}) | ||
|
||
it('throws Error if content table is not localized', () => { | ||
const strapi = { | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => ({ | ||
attributes: { | ||
localizations: {}, | ||
}, | ||
})), | ||
}, | ||
}, | ||
} | ||
|
||
const untranslatedService = service({ strapi }) | ||
|
||
return expect(async () => | ||
untranslatedService.getUntranslatedEntity({ uid: 'uid' }, {}) | ||
).rejects.toThrow('Content Type not localized') | ||
}) | ||
}) | ||
|
||
describe('getUntranslatedEntityIDs', () => { | ||
it('throws Error if Metadata is not found', () => { | ||
const strapi = { | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => null), | ||
}, | ||
}, | ||
} | ||
|
||
const untranslatedService = service({ strapi }) | ||
|
||
return expect(async () => | ||
untranslatedService.getUntranslatedEntityIDs({ uid: 'uid' }) | ||
).rejects.toThrow('Content Type does not exist') | ||
}) | ||
|
||
it('throws Error if content table is not localized', () => { | ||
const strapi = { | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => ({ | ||
attributes: { | ||
localizations: {}, | ||
}, | ||
})), | ||
}, | ||
}, | ||
} | ||
|
||
const untranslatedService = service({ strapi }) | ||
|
||
return expect(async () => | ||
untranslatedService.getUntranslatedEntityIDs({ uid: 'uid' }) | ||
).rejects.toThrow('Content Type not localized') | ||
}) | ||
}) | ||
|
||
describe('isFullyTranslated', () => { | ||
it('throws Error if Metadata is not found', () => { | ||
const strapi = { | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => null), | ||
}, | ||
}, | ||
} | ||
|
||
const untranslatedService = service({ strapi }) | ||
|
||
return expect(async () => | ||
untranslatedService.isFullyTranslated('uid', {}) | ||
).rejects.toThrow('Content Type does not exist') | ||
}) | ||
|
||
it('throws Error if content table is not localized', () => { | ||
const strapi = { | ||
db: { | ||
metadata: { | ||
get: jest.fn(() => ({ | ||
attributes: { | ||
localizations: {}, | ||
}, | ||
})), | ||
}, | ||
}, | ||
} | ||
|
||
const untranslatedService = service({ strapi }) | ||
|
||
return expect(async () => | ||
untranslatedService.isFullyTranslated('uid', {}) | ||
).rejects.toThrow('Content Type not localized') | ||
}) | ||
}) | ||
}) |
154 changes: 154 additions & 0 deletions
154
plugin/server/services/batch-translate/__tests__/BatchTranslateJob.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
'use strict' | ||
|
||
const { BatchTranslateJob } = require('../BatchTranslateJob') | ||
|
||
describe('BatchTranslateJob', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
Object.defineProperty(global, 'strapi', { | ||
value: require('../../../../__mocks__/initSetup')({ | ||
contentTypes: { | ||
nonTranslatedContentType: { | ||
pluginOptions: { i18n: { localized: false } }, | ||
}, | ||
translatedContentType: { | ||
pluginOptions: { i18n: { localized: true } }, | ||
}, | ||
}, | ||
}), | ||
writable: true, | ||
}) | ||
}) | ||
|
||
it("constructor throws if content type isn't localized", () => { | ||
expect(() => { | ||
new BatchTranslateJob({ | ||
id: 'id', | ||
contentType: 'nonTranslatedContentType', | ||
sourceLocale: 'sourceLocale', | ||
targetLocale: 'targetLocale', | ||
entityIds: ['entityIds'], | ||
status: 'status', | ||
autoPublish: false, | ||
}) | ||
}).toThrow('translate.batch-translate.content-type-not-localized') | ||
}) | ||
|
||
it('constructor does not throw if content type is localized', () => { | ||
expect(() => { | ||
new BatchTranslateJob({ | ||
id: 'id', | ||
contentType: 'translatedContentType', | ||
sourceLocale: 'sourceLocale', | ||
targetLocale: 'targetLocale', | ||
entityIds: ['entityIds'], | ||
status: 'status', | ||
autoPublish: false, | ||
}) | ||
}).not.toThrow() | ||
}) | ||
|
||
describe('start', () => { | ||
it('throws for status !== "created"', async () => { | ||
const job = new BatchTranslateJob({ | ||
id: 'id', | ||
contentType: 'translatedContentType', | ||
sourceLocale: 'sourceLocale', | ||
targetLocale: 'targetLocale', | ||
entityIds: ['entityIds'], | ||
status: 'status', | ||
autoPublish: false, | ||
}) | ||
|
||
expect(async () => job.start()).rejects.toThrow() | ||
}) | ||
|
||
it('does not throw for status === "created"', async () => { | ||
const job = new BatchTranslateJob({ | ||
id: 'id', | ||
contentType: 'translatedContentType', | ||
sourceLocale: 'sourceLocale', | ||
targetLocale: 'targetLocale', | ||
entityIds: ['entityIds'], | ||
status: 'created', | ||
autoPublish: false, | ||
}) | ||
|
||
expect(job.start()).resolves.not.toThrow() | ||
}) | ||
}) | ||
|
||
describe('status updates', () => { | ||
let job | ||
|
||
beforeEach(() => { | ||
job = new BatchTranslateJob({ | ||
id: 'id', | ||
contentType: 'translatedContentType', | ||
sourceLocale: 'sourceLocale', | ||
targetLocale: 'targetLocale', | ||
entityIds: ['entityIds'], | ||
status: 'created', | ||
autoPublish: false, | ||
}) | ||
}) | ||
|
||
it("cancel updates the status to 'cancelled' if it is running", async () => { | ||
const f = jest.fn() | ||
BatchTranslateJob.prototype.updateStatus = f | ||
|
||
await job.cancel() | ||
|
||
expect(f).toHaveBeenCalledWith('cancelled') | ||
}) | ||
|
||
it('cancel does nothing if job is not running', async () => { | ||
const f = jest.fn() | ||
BatchTranslateJob.prototype.updateStatus = f | ||
|
||
job.status = 'finished' | ||
await job.cancel() | ||
|
||
expect(f).not.toHaveBeenCalled() | ||
}) | ||
|
||
it("pause updates the status to 'paused' if it is running", async () => { | ||
const f = jest.fn() | ||
BatchTranslateJob.prototype.updateStatus = f | ||
|
||
await job.pause() | ||
|
||
expect(f).toHaveBeenCalledWith('paused') | ||
}) | ||
|
||
it('pause does nothing if job is not running', async () => { | ||
const f = jest.fn() | ||
BatchTranslateJob.prototype.updateStatus = f | ||
|
||
job.status = 'finished' | ||
|
||
await job.pause() | ||
|
||
expect(f).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('setup changes the status to setup and then running', async () => { | ||
const f = jest.fn((status) => (job.status = status)) | ||
BatchTranslateJob.prototype.updateStatus = f | ||
|
||
await job.setup() | ||
|
||
expect(f).toHaveBeenCalledWith('setup') | ||
expect(f).toHaveBeenCalledWith('running') | ||
}) | ||
|
||
it('start changes the status to finished after successful termination', async () => { | ||
const f = jest.fn((status) => (job.status = status)) | ||
BatchTranslateJob.prototype.updateStatus = f | ||
|
||
await job.start() | ||
|
||
expect(f).toHaveBeenCalledWith('finished') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict' | ||
|
||
const { provider, name, init } = require('../dummy-provider') | ||
|
||
describe('Dummy Provider', () => { | ||
it('meta data is correct', () => { | ||
expect(provider).toBe('dummy') | ||
expect(name).toBe('Dummy') | ||
}) | ||
|
||
describe('translation function', () => { | ||
let translate | ||
|
||
beforeAll(() => { | ||
translate = init().translate | ||
}) | ||
|
||
it('throws if targetLocale is not defined', () => { | ||
return expect(async () => | ||
translate({ sourceLocale: 'de', text: 'Lorem Ipsum' }) | ||
).rejects.toBeTruthy() | ||
}) | ||
|
||
it('throws if sourceLocale is not defined', () => { | ||
return expect(async () => | ||
translate({ targetLocale: 'de', text: 'Lorem Ipsum' }) | ||
).rejects.toBeTruthy() | ||
}) | ||
|
||
it('returns empty Array if no text is give', async () => { | ||
expect( | ||
await translate({ sourceLocale: 'de', targetLocale: 'en' }) | ||
).toEqual([]) | ||
}) | ||
|
||
it('return array of input if string is given', async () => { | ||
expect( | ||
await translate({ | ||
sourceLocale: 'de', | ||
targetLocale: 'anything', | ||
text: 'Give me this as an array', | ||
}) | ||
).toEqual(['Give me this as an array']) | ||
}) | ||
|
||
it('return input if array of strings is given', async () => { | ||
expect( | ||
await translate({ | ||
sourceLocale: 'de', | ||
targetLocale: 'anything', | ||
text: ['Give', 'me', 'this back'], | ||
}) | ||
).toEqual(['Give', 'me', 'this back']) | ||
}) | ||
}) | ||
|
||
it('usage function is static', async () => { | ||
let { usage, translate } = init() | ||
|
||
const usageResult1 = await usage() | ||
|
||
await translate({ | ||
sourceLocale: 'de', | ||
targetLocale: 'en', | ||
text: 'Some random long text that should increase the usage, but does not, because this is the dummy provider.', | ||
}) | ||
|
||
const usageResult2 = await usage() | ||
|
||
expect(usageResult1).toEqual(usageResult2) | ||
}) | ||
}) |
Oops, something went wrong.