Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: project insights read model test (#6657)
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/lib/features/project-insights/project-insights-read-model.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,66 @@ | ||
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init'; | ||
import getLogger from '../../../test/fixtures/no-logger'; | ||
import type { IUser } from '../../types'; | ||
import type { IProjectInsightsReadModel } from './project-insights-read-model-type'; | ||
import { | ||
type ChangeRequestDBState, | ||
ProjectInsightsReadModel, | ||
} from './project-insights-read-model'; | ||
|
||
let projectInsightsReadModel: IProjectInsightsReadModel; | ||
let user: IUser; | ||
let db: ITestDb; | ||
const projectId = 'default'; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('project_insights_read_model', getLogger); | ||
projectInsightsReadModel = new ProjectInsightsReadModel(db.rawDatabase); | ||
user = await db.stores.userStore.insert({ | ||
username: 'test', | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await db.rawDatabase.table('change_requests').delete(); | ||
}); | ||
|
||
const createChangeRequest = (id: number, state: string) => | ||
db.rawDatabase.table('change_requests').insert({ | ||
id, | ||
state, | ||
environment: 'default', | ||
project: projectId, | ||
created_by: user.id, | ||
}); | ||
|
||
test('can read change request status counts', async () => { | ||
const states: ChangeRequestDBState[] = [ | ||
'Approved', | ||
'Approved', | ||
'Applied', | ||
'Rejected', | ||
'Scheduled', | ||
'In review', | ||
'Draft', | ||
'Cancelled', | ||
]; | ||
await Promise.all( | ||
states.map((state, id) => createChangeRequest(id, state)), | ||
); | ||
|
||
const changeRequests = | ||
await projectInsightsReadModel.getChangeRequests(projectId); | ||
|
||
expect(changeRequests).toEqual({ | ||
total: 6, | ||
approved: 2, | ||
applied: 1, | ||
rejected: 1, | ||
reviewRequired: 1, | ||
scheduled: 1, | ||
}); | ||
}); |
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