123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import {IncidentFixture} from 'sentry-fixture/incident';
- import {IncidentStatsFixture} from 'sentry-fixture/incidentStats';
- import {MetricRuleFixture} from 'sentry-fixture/metricRule';
- import {ProjectFixture} from 'sentry-fixture/project';
- import {TeamFixture} from 'sentry-fixture/team';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {act, render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
- import selectEvent from 'sentry-test/selectEvent';
- import ProjectsStore from 'sentry/stores/projectsStore';
- import TeamStore from 'sentry/stores/teamStore';
- import type {Organization} from 'sentry/types/organization';
- import AlertsContainer from 'sentry/views/alerts';
- import IncidentsList from 'sentry/views/alerts/list/incidents';
- describe('IncidentsList', () => {
- const projects1 = ['a', 'b', 'c'];
- const projects2 = ['c', 'd'];
- interface Props {
- orgOverride?: Partial<Organization>;
- }
- const renderComponent = ({orgOverride}: Props = {}) => {
- const {organization, routerProps, router} = initializeOrg({
- organization: {features: ['incidents'], ...orgOverride},
- });
- return {
- component: render(
- <AlertsContainer>
- <IncidentsList {...routerProps} organization={organization} />
- </AlertsContainer>,
- {router, organization}
- ),
- router,
- };
- };
- beforeEach(() => {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/incidents/',
- body: [
- IncidentFixture({
- id: '123',
- identifier: '1',
- title: 'First incident',
- projects: projects1,
- }),
- IncidentFixture({
- id: '342',
- identifier: '2',
- title: 'Second incident',
- projects: projects2,
- }),
- ],
- });
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/incidents/2/stats/',
- body: IncidentStatsFixture({
- totalEvents: 1000,
- uniqueUsers: 32,
- eventStats: {
- data: [[1591390293327, [{count: 42}]]],
- },
- }),
- });
- const projects = [
- ProjectFixture({slug: 'a', platform: 'javascript'}),
- ProjectFixture({slug: 'b'}),
- ProjectFixture({slug: 'c'}),
- ProjectFixture({slug: 'd'}),
- ];
- act(() => ProjectsStore.loadInitialData(projects));
- });
- afterEach(() => {
- act(() => ProjectsStore.reset());
- MockApiClient.clearMockResponses();
- });
- it('displays list', async () => {
- renderComponent();
- const items = await screen.findAllByTestId('alert-title');
- expect(items).toHaveLength(2);
- expect(within(items[0]).getByText('First incident')).toBeInTheDocument();
- expect(within(items[1]).getByText('Second incident')).toBeInTheDocument();
- const projectBadges = screen.getAllByTestId('badge-display-name');
- expect(within(projectBadges[0]).getByText('a')).toBeInTheDocument();
- });
- it('displays empty state (first time experience)', async () => {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/incidents/',
- body: [],
- });
- const rulesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/alert-rules/',
- body: [],
- });
- const promptsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/prompts-activity/',
- body: {data: {dismissed_ts: null}},
- });
- const promptsUpdateMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/prompts-activity/',
- method: 'PUT',
- });
- renderComponent();
- expect(await screen.findByText('More signal, less noise')).toBeInTheDocument();
- expect(rulesMock).toHaveBeenCalledTimes(1);
- expect(promptsMock).toHaveBeenCalledTimes(1);
- expect(promptsUpdateMock).toHaveBeenCalledTimes(1);
- });
- it('displays empty state (rules not yet created)', async () => {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/incidents/',
- body: [],
- });
- const rulesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/alert-rules/',
- body: [],
- });
- const promptsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/prompts-activity/',
- body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
- });
- renderComponent();
- expect(
- await screen.findByText('No incidents exist for the current query.')
- ).toBeInTheDocument();
- expect(rulesMock).toHaveBeenCalledTimes(1);
- expect(promptsMock).toHaveBeenCalledTimes(1);
- });
- it('displays empty state (rules created)', async () => {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/incidents/',
- body: [],
- });
- const rulesMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/alert-rules/',
- body: [{id: 1}],
- });
- const promptsMock = MockApiClient.addMockResponse({
- url: '/organizations/org-slug/prompts-activity/',
- body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
- });
- renderComponent();
- expect(
- await screen.findByText('No incidents exist for the current query.')
- ).toBeInTheDocument();
- expect(rulesMock).toHaveBeenCalledTimes(1);
- expect(promptsMock).toHaveBeenCalledTimes(0);
- });
- it('filters by status', async () => {
- const {router} = renderComponent();
- await selectEvent.select(await screen.findByText('Status'), 'Active');
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- status: 'open',
- },
- })
- );
- });
- it('disables the new alert button for those without alert:write', async () => {
- const noAccessOrg = {
- access: [],
- };
- renderComponent({orgOverride: noAccessOrg});
- expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
- 'aria-disabled',
- 'true'
- );
- });
- it('does not disable the new alert button for those with alert:write', async () => {
- // Enabled with access
- renderComponent();
- expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
- 'aria-disabled',
- 'false'
- );
- });
- it('searches by name', async () => {
- const {router} = renderComponent();
- const input = await screen.findByPlaceholderText('Search by name');
- expect(input).toBeInTheDocument();
- const testQuery = 'test name';
- await userEvent.type(input, `${testQuery}{enter}`);
- expect(router.push).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- title: testQuery,
- },
- })
- );
- });
- it('displays owner from alert rule', async () => {
- const team = TeamFixture();
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/incidents/',
- body: [
- IncidentFixture({
- id: '123',
- identifier: '1',
- title: 'First incident',
- projects: projects1,
- alertRule: MetricRuleFixture({owner: `team:${team.id}`}),
- }),
- ],
- });
- TeamStore.getById = jest.fn().mockReturnValue(team);
- renderComponent();
- expect(await screen.findByText(team.name)).toBeInTheDocument();
- });
- });
|