Skip to content

Commit

Permalink
test: standardize user actions test file formatting
Browse files Browse the repository at this point in the history
- Applied consistent code style with semicolons
- Cleaned up import statements
- Improved code formatting and readability
- Maintained existing test logic and coverage
  • Loading branch information
alvarosabu committed Jan 30, 2025
1 parent 335f859 commit 3228eca
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/commands/user/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
import { getUser } from './actions'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { APIError } from '../../utils'
import { FetchError } from '../../utils/fetch'
import { getUser } from './actions';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { APIError } from '../../utils';
import { FetchError } from '../../utils/fetch';

const handlers = [
http.get('https://api.storyblok.com/v1/users/me', async ({ request }) => {
const token = request.headers.get('Authorization')
const token = request.headers.get('Authorization');
if (token === 'valid-token') {
return HttpResponse.json({ data: 'user data' })
return HttpResponse.json({ data: 'user data' });
}
return new HttpResponse('Unauthorized', { status: 401 })
return new HttpResponse('Unauthorized', { status: 401 });
}),
]
];

const server = setupServer(...handlers)
const server = setupServer(...handlers);

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));

afterEach(() => server.resetHandlers())
afterAll(() => server.close())
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

describe('user actions', () => {
beforeEach(() => {
vi.clearAllMocks()
})
vi.clearAllMocks();
});

describe('getUser', () => {
it('should get user successfully with a valid token', async () => {
const mockResponse = { data: 'user data' }
const result = await getUser('valid-token', 'eu')
expect(result).toEqual(mockResponse)
})
})
const mockResponse = { data: 'user data' };
const result = await getUser('valid-token', 'eu');
expect(result).toEqual(mockResponse);
});
});

it('should throw an masked error for invalid token', async () => {
const error = new FetchError('Non-JSON response', {
status: 401,
statusText: 'Unauthorized',
data: null,
})
});
await expect(getUser('invalid-token', 'eu')).rejects.toThrow(
new APIError('unauthorized', 'get_user', error, `The token provided inva********* is invalid.
Please make sure you are using the correct token and try again.`),
)
})
);
});

it('should throw a network error if response is empty (network)', async () => {
server.use(
http.get('https://api.storyblok.com/v1/users/me', () => {
return new HttpResponse(null, { status: 500 })
return new HttpResponse(null, { status: 500 });
}),
)
);
await expect(getUser('any-token', 'eu')).rejects.toThrow(
'No response from server, please check if you are correctly connected to internet',
)
})
})
);
});
});

0 comments on commit 3228eca

Please sign in to comment.