-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: standardize user actions test file formatting
- Applied consistent code style with semicolons - Cleaned up import statements - Improved code formatting and readability - Maintained existing test logic and coverage
- Loading branch information
1 parent
335f859
commit 3228eca
Showing
1 changed file
with
29 additions
and
29 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 |
---|---|---|
@@ -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', | ||
) | ||
}) | ||
}) | ||
); | ||
}); | ||
}); |