Skip to content

Commit

Permalink
Merge pull request #471 from Fekide/470-bug-missing-query-handler-in-…
Browse files Browse the repository at this point in the history
…unit-tests

470 bug missing query handler in unit tests
  • Loading branch information
layaxx authored Nov 29, 2024
2 parents 1eedc30 + 199cac0 commit 8e5f787
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ jobs:
cache: 'yarn'
- run: yarn
- run: yarn test
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./plugin/coverage/clover.xml,./providers/deepl/coverage/clover.xml,./providers/libretranslate/coverage/clover.xml
flags: unittests
name: codecov-umbrella
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
12 changes: 6 additions & 6 deletions plugin/__mocks__/initSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ module.exports = ({
'batch-translate-job': () => {
const uid = 'plugin::translate.batch-translate-job'
return {
findOne: this.db.query(uid).findOne,
find: this.db.query(uid).findMany,
count: this.db.query(uid).count,
create: this.db.query(uid).create,
update: this.db.query(uid).update,
delete: this.db.query(uid).delete,
findOne: mock.db.query(uid).findOne,
find: mock.db.query(uid).findMany,
count: mock.db.query(uid).count,
create: mock.db.query(uid).create,
update: mock.db.query(uid).update,
delete: mock.db.query(uid).delete,
}
},
},
Expand Down
119 changes: 119 additions & 0 deletions plugin/server/services/__tests__/untranslated-service.test.js
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')
})
})
})
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')
})
})
})
72 changes: 72 additions & 0 deletions plugin/server/utils/__tests__/dummy-provider.test.js
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)
})
})
Loading

0 comments on commit 8e5f787

Please sign in to comment.