-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RSN-53] Add pre-commit hook to run fmt and lint (#44)
* chore: configure linters for the project * ci: add pre-commit hook to run fmt and lint * ci: unblock ci caused by yarn.lock error * chore: remove unnecessary added files * fix: dotnet format now can fail pre-commit
- Loading branch information
Showing
34 changed files
with
2,666 additions
and
1,049 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
command_exists () { | ||
command -v "$1" >/dev/null 2>&1 | ||
} | ||
|
||
# Workaround for Windows 10, Git Bash, and Yarn | ||
if command_exists winpty && test -t 1; then | ||
exec < /dev/tty | ||
fi |
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,9 @@ | ||
# .husky/pre-commit | ||
. "$(dirname -- "$0")/common.sh" | ||
|
||
(cd ./Client/reasn-client && yarn lint-staged) | ||
(cd ./Server/ReasnAPI && dotnet format) | ||
if ! git diff --quiet; then | ||
echo "🚫 dotnet format made changes, commit aborted." | ||
exit 1 | ||
fi |
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,10 @@ | ||
module.exports = { | ||
extends: ["expo", "prettier"], | ||
plugins: ["prettier"], | ||
rules: { | ||
"prettier/prettier": "error", | ||
}, | ||
globals: { | ||
__dirname: true, | ||
}, | ||
}; |
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
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,3 +1,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
"extends": ["next/core-web-vitals", "prettier"] | ||
} |
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
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
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,6 +1,6 @@ | ||
module.exports = { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
}; | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
}; |
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,12 +1,11 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
module.exports = { | ||
content: [ | ||
'./app/**/*.{js,jsx,ts,tsx}', | ||
'../../packages/ui/src/**/*.{js,jsx,ts,tsx}' | ||
"./app/**/*.{js,jsx,ts,tsx}", | ||
"../../packages/ui/src/**/*.{js,jsx,ts,tsx}", | ||
], | ||
theme: { | ||
extend: {}, | ||
}, | ||
plugins: [], | ||
} | ||
|
||
}; |
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
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
112 changes: 59 additions & 53 deletions
112
Client/reasn-client/packages/common/__tests__/services/apiServices.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 |
---|---|---|
@@ -1,80 +1,86 @@ | ||
import { sendRequest } from '@reasn/common/services/apiServices'; | ||
import { getAuthDataFromSessionStorage } from '@reasn/common/services/authorizationServices'; | ||
import { sendRequest } from "@reasn/common/services/apiServices"; | ||
import { getAuthDataFromSessionStorage } from "@reasn/common/services/authorizationServices"; | ||
import { AuthData } from "@reasn/common/interfaces/AuthData"; | ||
import { HttpMethod } from '@reasn/common/enums/serviceEnums'; | ||
import { describe, expect, it, jest, beforeEach } from '@jest/globals'; | ||
import fetch from 'cross-fetch'; | ||
import ApiConnectionError from '@reasn/common/errors/ApiConnectionError'; | ||
import ApiAuthorizationError from '@reasn/common/errors/ApiAuthorizationError'; | ||
import { HttpMethod } from "@reasn/common/enums/serviceEnums"; | ||
import { describe, expect, it, jest, beforeEach } from "@jest/globals"; | ||
import fetch from "cross-fetch"; | ||
import ApiConnectionError from "@reasn/common/errors/ApiConnectionError"; | ||
import ApiAuthorizationError from "@reasn/common/errors/ApiAuthorizationError"; | ||
|
||
jest.mock('cross-fetch'); | ||
jest.mock('@reasn/common/services/authorizationServices'); | ||
jest.mock("cross-fetch"); | ||
jest.mock("@reasn/common/services/authorizationServices"); | ||
|
||
describe('sendRequest', () => { | ||
beforeEach(() => { | ||
(fetch as jest.Mock).mockClear(); | ||
(getAuthDataFromSessionStorage as jest.Mock).mockClear(); | ||
}); | ||
describe("sendRequest", () => { | ||
beforeEach(() => { | ||
(fetch as jest.Mock).mockClear(); | ||
(getAuthDataFromSessionStorage as jest.Mock).mockClear(); | ||
}); | ||
|
||
it('should return data when response is ok', async () => { | ||
const mockData = { key: 'value' }; | ||
it("should return data when response is ok", async () => { | ||
const mockData = { key: "value" }; | ||
(fetch as jest.Mock).mockImplementationOnce(() => ({ | ||
ok: true, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
ok: true, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
|
||
const data = await sendRequest('http://example.com', HttpMethod.GET); | ||
const data = await sendRequest("http://example.com", HttpMethod.GET); | ||
|
||
expect(data).toEqual(mockData); | ||
}); | ||
|
||
it('should throw an API error when response is not ok', async () => { | ||
const mockData = { message: 'Error message' }; | ||
it("should throw an API error when response is not ok", async () => { | ||
const mockData = { message: "Error message" }; | ||
(fetch as jest.Mock).mockImplementationOnce(() => ({ | ||
ok: false, | ||
status: 500, | ||
statusText: mockData.message, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
ok: false, | ||
status: 500, | ||
statusText: mockData.message, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
|
||
await expect(sendRequest('http://example.com', HttpMethod.GET)).rejects.toThrow(ApiConnectionError); | ||
await expect( | ||
sendRequest("http://example.com", HttpMethod.GET), | ||
).rejects.toThrow(ApiConnectionError); | ||
}); | ||
|
||
it('should include auth token in headers when authRequired is true', async () => { | ||
const mockData = { key: 'value' }; | ||
it("should include auth token in headers when authRequired is true", async () => { | ||
const mockData = { key: "value" }; | ||
(fetch as jest.Mock).mockImplementationOnce(() => ({ | ||
ok: true, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
(getAuthDataFromSessionStorage as jest.Mock).mockReturnValueOnce({ token: 'token' } as AuthData); | ||
ok: true, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
(getAuthDataFromSessionStorage as jest.Mock).mockReturnValueOnce({ | ||
token: "token", | ||
} as AuthData); | ||
|
||
await sendRequest('http://example.com', HttpMethod.GET, {}, true); | ||
await sendRequest("http://example.com", HttpMethod.GET, {}, true); | ||
|
||
expect(fetch).toHaveBeenCalledWith('http://example.com', { | ||
method: HttpMethod.GET, | ||
headers: { Authorization: 'Bearer token' }, | ||
}); | ||
expect(fetch).toHaveBeenCalledWith("http://example.com", { | ||
method: HttpMethod.GET, | ||
headers: { Authorization: "Bearer token" }, | ||
}); | ||
}); | ||
|
||
it('should have correct fetch options', async () => { | ||
const mockData = { key: 'value' }; | ||
it("should have correct fetch options", async () => { | ||
const mockData = { key: "value" }; | ||
(fetch as jest.Mock).mockImplementationOnce(() => ({ | ||
ok: true, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
ok: true, | ||
json: () => Promise.resolve(mockData), | ||
})); | ||
|
||
await sendRequest('http://example.com', HttpMethod.POST, { key: 'value' }); | ||
await sendRequest("http://example.com", HttpMethod.POST, { key: "value" }); | ||
|
||
expect(fetch).toHaveBeenCalledWith('http://example.com', { | ||
method: HttpMethod.POST, | ||
headers: {}, | ||
body: JSON.stringify({ key: 'value' }), | ||
}); | ||
expect(fetch).toHaveBeenCalledWith("http://example.com", { | ||
method: HttpMethod.POST, | ||
headers: {}, | ||
body: JSON.stringify({ key: "value" }), | ||
}); | ||
}); | ||
|
||
it('should throw an AUTH error when authRequired is true and no auth data is found', async () => { | ||
it("should throw an AUTH error when authRequired is true and no auth data is found", async () => { | ||
(getAuthDataFromSessionStorage as jest.Mock).mockReturnValueOnce(null); | ||
|
||
await expect(sendRequest('http://example.com', HttpMethod.GET, {}, true)).rejects.toThrow(ApiAuthorizationError); | ||
await expect( | ||
sendRequest("http://example.com", HttpMethod.GET, {}, true), | ||
).rejects.toThrow(ApiAuthorizationError); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.