This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit test announcements-backend (#87)
* test: unit test backend Signed-off-by: Kurt King <[email protected]> * chore: uncomment expect statement Signed-off-by: Kurt King <[email protected]> * chore: move dep to dev-deps Signed-off-by: Kurt King <[email protected]> * chore: include changeset Signed-off-by: Kurt King <[email protected]> --------- Signed-off-by: Kurt King <[email protected]>
- Loading branch information
Showing
10 changed files
with
901 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@procore-oss/backstage-plugin-announcements-backend': patch | ||
--- | ||
|
||
Improves test coverage significantly for the backend plugin |
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
81 changes: 81 additions & 0 deletions
81
plugins/announcements-backend/src/search/AnnouncementCollatorFactory.test.ts
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,81 @@ | ||
import { AnnouncementCollatorFactory } from './AnnouncementCollatorFactory'; | ||
import { Readable } from 'stream'; | ||
import { getVoidLogger } from '@backstage/backend-common'; | ||
import { TestPipeline } from '@backstage/plugin-search-backend-node'; | ||
import { setupRequestMockHandlers } from '@backstage/test-utils'; | ||
import { setupServer } from 'msw/node'; | ||
import { rest } from 'msw'; | ||
|
||
const mockAnnouncements = { | ||
count: 3, | ||
results: [ | ||
{ | ||
id: '1', | ||
title: 'title', | ||
publisher: 'publisher1', | ||
body: 'body', | ||
excerpt: 'excerpt', | ||
created_at: 'created_at', | ||
}, | ||
{ | ||
id: '2', | ||
title: 'title', | ||
publisher: 'publisher2', | ||
body: 'body', | ||
excerpt: 'excerpt', | ||
created_at: 'created_at', | ||
}, | ||
{ | ||
id: '3', | ||
title: 'title', | ||
publisher: 'publisher3', | ||
body: 'body', | ||
excerpt: 'excerpt', | ||
created_at: 'created_at', | ||
}, | ||
], | ||
}; | ||
|
||
describe('AnnouncementCollatorFactory', () => { | ||
const logger = getVoidLogger(); | ||
const mockDiscoveryApi = { | ||
getBaseUrl: jest.fn().mockReturnValue('http://localhost:7007/api'), | ||
}; | ||
|
||
const factory = AnnouncementCollatorFactory.fromConfig({ | ||
logger, | ||
discoveryApi: mockDiscoveryApi, | ||
}); | ||
|
||
it('has expected type', () => { | ||
expect(factory.type).toBe('announcements'); | ||
}); | ||
|
||
describe('getCollator', () => { | ||
const worker = setupServer(); | ||
setupRequestMockHandlers(worker); | ||
|
||
let collator: Readable; | ||
beforeEach(async () => { | ||
collator = await factory.getCollator(); | ||
worker.use( | ||
rest.get('http://localhost:7007/api/announcements', (_, res, ctx) => | ||
res(ctx.status(200), ctx.json(mockAnnouncements)), | ||
), | ||
); | ||
}); | ||
|
||
it('should return a Readable stream', async () => { | ||
collator = await factory.getCollator(); | ||
expect(collator).toBeInstanceOf(Readable); | ||
}); | ||
|
||
it('runs against announcements', async () => { | ||
collator = await factory.getCollator(); | ||
const pipeline = TestPipeline.fromCollator(collator); | ||
const { documents } = await pipeline.execute(); | ||
expect(mockDiscoveryApi.getBaseUrl).toHaveBeenCalledWith('announcements'); | ||
expect(documents).toHaveLength(mockAnnouncements.results.length); | ||
}); | ||
}); | ||
}); |
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,68 @@ | ||
import { setupRequestMockHandlers } from '@backstage/test-utils'; | ||
import { AnnouncementsClient } from './api'; | ||
import { rest } from 'msw'; | ||
import { setupServer } from 'msw/node'; | ||
|
||
describe('AnnouncementsClient', () => { | ||
const server = setupServer(); | ||
const mockBaseUrl = 'http://localhost:7007/api/'; | ||
const discoveryApi = { getBaseUrl: async () => mockBaseUrl }; | ||
|
||
setupRequestMockHandlers(server); | ||
|
||
let client: AnnouncementsClient; | ||
beforeEach(() => { | ||
client = new AnnouncementsClient({ discoveryApi }); | ||
server.listen(); | ||
}); | ||
|
||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
it('handles no announcements', async () => { | ||
server.use( | ||
rest.get(`${mockBaseUrl}/announcements`, (_, res, ctx) => | ||
res(ctx.status(200), ctx.json({ count: 0, results: [] })), | ||
), | ||
); | ||
expect(await client.announcements()).toEqual([]); | ||
}); | ||
|
||
it('returns announcements', async () => { | ||
const announcements = [ | ||
{ | ||
id: '1', | ||
title: 'announcement1', | ||
excerpt: 'excerpt', | ||
body: 'body', | ||
publisher: 'publisher', | ||
created_at: '2021-01-01T00:00:00Z', | ||
}, | ||
{ | ||
id: '1', | ||
title: 'announcement2', | ||
excerpt: 'excerpt', | ||
body: 'body', | ||
publisher: 'publisher2', | ||
created_at: '2021-01-02T00:00:00Z', | ||
}, | ||
]; | ||
server.use( | ||
rest.get(`${mockBaseUrl}/announcements`, (_, res, ctx) => | ||
res(ctx.status(200), ctx.json({ count: 2, results: announcements })), | ||
), | ||
); | ||
expect(await client.announcements()).toEqual(announcements); | ||
}); | ||
|
||
it('throws on error', async () => { | ||
server.use( | ||
rest.get(`${mockBaseUrl}/announcements`, (_, res, ctx) => | ||
res(ctx.status(500)), | ||
), | ||
); | ||
await expect(client.announcements()).rejects.toThrow( | ||
'Request failed with 500 Error', | ||
); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
plugins/announcements-backend/src/service/announcementsContextBuilder.test.ts
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,34 @@ | ||
import { PermissionEvaluator } from '@backstage/plugin-permission-common'; | ||
import { buildAnnouncementsContext } from './announcementsContextBuilder'; | ||
import { getVoidLogger } from '@backstage/backend-common'; | ||
import { initializePersistenceContext } from './persistence/persistenceContext'; | ||
|
||
jest.mock('./persistence/persistenceContext', () => ({ | ||
initializePersistenceContext: jest.fn(), | ||
})); | ||
|
||
describe('buildAnnouncementsContext', () => { | ||
it('returns context with logger, persistenceContext, and permissions properties', async () => { | ||
const logger = getVoidLogger(); | ||
const database = { | ||
getClient: jest.fn(), | ||
url: 'url', | ||
}; | ||
const permissions: PermissionEvaluator = { | ||
authorize: jest.fn(), | ||
authorizeConditional: jest.fn(), | ||
}; | ||
|
||
const context = await buildAnnouncementsContext({ | ||
logger, | ||
database, | ||
permissions, | ||
}); | ||
|
||
expect(context).toStrictEqual({ | ||
logger, | ||
persistenceContext: await initializePersistenceContext(database), | ||
permissions, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.