123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import {ApiTokenFixture} from 'sentry-fixture/apiToken';
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import * as indicators from 'sentry/actionCreators/indicator';
- import ApiTokenDetails from 'sentry/views/settings/account/apiTokenDetails';
- describe('ApiNewToken', function () {
- MockApiClient.clearMockResponses();
- it('renders', function () {
- MockApiClient.addMockResponse({
- method: 'GET',
- url: `/api-tokens/1/`,
- body: ApiTokenFixture({id: '1', name: 'token1'}),
- });
- render(<ApiTokenDetails params={{tokenId: '1'}} />);
- });
- it('renames token to new name', async function () {
- MockApiClient.clearMockResponses();
- jest.spyOn(indicators, 'addSuccessMessage');
- const mock1 = MockApiClient.addMockResponse({
- method: 'GET',
- url: `/api-tokens/1/`,
- body: ApiTokenFixture({id: '1', name: 'token1'}),
- });
- render(<ApiTokenDetails params={{tokenId: '1'}} />);
- await waitFor(() => expect(mock1).toHaveBeenCalledTimes(1));
- const assignMock = MockApiClient.addMockResponse({
- method: 'PUT',
- url: `/api-tokens/1/`,
- });
- await userEvent.type(screen.getByRole('textbox', {name: /name/i}), ' new');
- await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
- await waitFor(() =>
- expect(assignMock).toHaveBeenCalledWith(
- '/api-tokens/1/',
- expect.objectContaining({
- method: 'PUT',
- data: expect.objectContaining({
- name: 'token1 new',
- }),
- })
- )
- );
- expect(indicators.addSuccessMessage).toHaveBeenCalled();
- });
- it('removes token name', async function () {
- MockApiClient.clearMockResponses();
- jest.spyOn(indicators, 'addSuccessMessage');
- const mock1 = MockApiClient.addMockResponse({
- method: 'GET',
- url: `/api-tokens/1/`,
- body: ApiTokenFixture({id: '1', name: 'token1'}),
- });
- render(<ApiTokenDetails params={{tokenId: '1'}} />);
- await waitFor(() => expect(mock1).toHaveBeenCalledTimes(1));
- const assignMock = MockApiClient.addMockResponse({
- method: 'PUT',
- url: `/api-tokens/1/`,
- });
- await userEvent.clear(screen.getByRole('textbox', {name: /name/i}));
- await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
- await waitFor(() =>
- expect(assignMock).toHaveBeenCalledWith(
- '/api-tokens/1/',
- expect.objectContaining({
- method: 'PUT',
- data: expect.objectContaining({
- name: '',
- }),
- })
- )
- );
- expect(indicators.addSuccessMessage).toHaveBeenCalled();
- });
- it('does not accept long name', async function () {
- MockApiClient.clearMockResponses();
- jest.spyOn(indicators, 'addErrorMessage');
- const mock1 = MockApiClient.addMockResponse({
- method: 'GET',
- url: `/api-tokens/1/`,
- body: ApiTokenFixture({id: '1', name: 'token1'}),
- });
- render(<ApiTokenDetails params={{tokenId: '1'}} />);
- await waitFor(() => expect(mock1).toHaveBeenCalledTimes(1));
- const assignMock = MockApiClient.addMockResponse({
- method: 'PUT',
- url: `/api-tokens/1/`,
- statusCode: 400,
- });
- await userEvent.type(
- screen.getByRole('textbox', {name: /name/i}),
- 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in'
- );
- await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
- await waitFor(() =>
- expect(assignMock).toHaveBeenCalledWith(
- '/api-tokens/1/',
- expect.objectContaining({
- method: 'PUT',
- data: expect.objectContaining({
- name: 'token1Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in',
- }),
- })
- )
- );
- expect(indicators.addErrorMessage).toHaveBeenCalled();
- });
- });
|