#708 apply strictier linting rules (#717)

* #708 apply strictier linting rules and fix simple eslint bugs

* #708 fix eslint errors relate to promise

* #708 fix eslint import/no-extraneous-dependencies

* #708 fix eslint errors of react-hook

* #708 enable eslint check for typescript

---------

Co-authored-by: lethemanh <lethemanh@lethemanhs-MacBook-Pro.local>
This commit is contained in:
lethemanh
2026-04-01 22:15:10 +07:00
committed by GitHub
parent 2bff6aae78
commit cadfa70e60
321 changed files with 27452 additions and 27600 deletions
@@ -1,234 +1,234 @@
import { getCalendarsListAsync } from "@/features/Calendars/services/getCalendarsListAsync";
import { getOpenPaasUser } from "@/features/User/userAPI";
import { fetchOwnerData } from "@/features/Calendars/services/helpers";
import { getCalendars } from "@/features/Calendars/CalendarApi";
import { formatReduxError } from "@/utils/errorUtils";
import { normalizeCalendar } from "@/features/Calendars/utils/normalizeCalendar";
import { getCalendarsListAsync } from '@/features/Calendars/services/getCalendarsListAsync'
import { getOpenPaasUser } from '@/features/User/userAPI'
import { fetchOwnerData } from '@/features/Calendars/services/helpers'
import { getCalendars } from '@/features/Calendars/CalendarApi'
import { formatReduxError } from '@/utils/errorUtils'
import { normalizeCalendar } from '@/features/Calendars/utils/normalizeCalendar'
jest.mock("@/features/User/userAPI");
jest.mock("@/features/Calendars/services/helpers");
jest.mock("@/features/Calendars/CalendarApi");
jest.mock("@/utils/errorUtils");
jest.mock("@/features/Calendars/utils/normalizeCalendar");
jest.mock("@/utils/getAccessiblePair", () => ({
getAccessiblePair: jest.fn().mockReturnValue("#FFF"),
}));
jest.mock('@/features/User/userAPI')
jest.mock('@/features/Calendars/services/helpers')
jest.mock('@/features/Calendars/CalendarApi')
jest.mock('@/utils/errorUtils')
jest.mock('@/features/Calendars/utils/normalizeCalendar')
jest.mock('@/utils/getAccessiblePair', () => ({
getAccessiblePair: jest.fn().mockReturnValue('#FFF')
}))
jest.mock("@mui/material/styles", () => ({
createTheme: jest.fn().mockReturnValue({}),
}));
jest.mock('@mui/material/styles', () => ({
createTheme: jest.fn().mockReturnValue({})
}))
const mockedGetOpenPaasUser = getOpenPaasUser as jest.Mock;
const mockedFetchOwnerData = fetchOwnerData as jest.Mock;
const mockedGetCalendars = getCalendars as jest.Mock;
const mockedFormatReduxError = formatReduxError as jest.Mock;
const mockedNormalizeCalendar = normalizeCalendar as jest.Mock;
const mockedGetOpenPaasUser = getOpenPaasUser as jest.Mock
const mockedFetchOwnerData = fetchOwnerData as jest.Mock
const mockedGetCalendars = getCalendars as jest.Mock
const mockedFormatReduxError = formatReduxError as jest.Mock
const mockedNormalizeCalendar = normalizeCalendar as jest.Mock
describe("getCalendarsListAsync", () => {
let dispatch: jest.Mock;
let getState: jest.Mock;
describe('getCalendarsListAsync', () => {
let dispatch: jest.Mock
let getState: jest.Mock
beforeEach(() => {
dispatch = jest.fn();
getState = jest.fn();
jest.clearAllMocks();
dispatch = jest.fn()
getState = jest.fn()
jest.clearAllMocks()
mockedFormatReduxError.mockImplementation((err) => {
if (err?.message) return err.message;
if (typeof err === "string") return err;
return JSON.stringify(err);
});
});
mockedFormatReduxError.mockImplementation(err => {
if (err?.message) return err.message
if (typeof err === 'string') return err
return JSON.stringify(err)
})
})
it("should handle successful execution and merge with existing calendars", async () => {
it('should handle successful execution and merge with existing calendars', async () => {
getState.mockReturnValue({
calendars: {
list: {
"cal-existing": {
id: "cal-existing",
color: { light: "red", dark: "#FFF" },
events: { "event-1": {} },
},
},
'cal-existing': {
id: 'cal-existing',
color: { light: 'red', dark: '#FFF' },
events: { 'event-1': {} }
}
}
},
user: {
userData: { openpaasId: "user-123" },
},
});
userData: { openpaasId: 'user-123' }
}
})
const mockCalendarsResponse = {
_embedded: {
"dav:calendar": [{ id: "cal-existing" }, { id: "cal-new" }],
},
};
mockedGetCalendars.mockResolvedValue(mockCalendarsResponse);
'dav:calendar': [{ id: 'cal-existing' }, { id: 'cal-new' }]
}
}
mockedGetCalendars.mockResolvedValue(mockCalendarsResponse)
mockedNormalizeCalendar
.mockReturnValueOnce({
cal: { "dav:name": "Existing Cal", "apple:color": "blue" },
id: "cal-existing",
ownerId: "user-123",
description: "old cal",
cal: { 'dav:name': 'Existing Cal', 'apple:color': 'blue' },
id: 'cal-existing',
ownerId: 'user-123',
description: 'old cal',
delegated: false,
link: "/link/1",
link: '/link/1',
visibility: 1,
access: 3,
access: 3
})
.mockReturnValueOnce({
cal: { "dav:name": "New Cal" },
id: "cal-new",
ownerId: "user-456",
description: "new cal",
cal: { 'dav:name': 'New Cal' },
id: 'cal-new',
ownerId: 'user-456',
description: 'new cal',
delegated: true,
link: "/link/2",
link: '/link/2',
visibility: 2,
access: 2,
invite: [{ href: "", principal: "", access: 3, inviteStatus: 1 }],
});
invite: [{ href: '', principal: '', access: 3, inviteStatus: 1 }]
})
mockedFetchOwnerData
.mockResolvedValueOnce({
firstname: "John",
lastname: "Doe",
emails: ["john@example.com"],
firstname: 'John',
lastname: 'Doe',
emails: ['john@example.com']
})
.mockResolvedValueOnce({
firstname: "Jane",
lastname: "Smith",
emails: ["jane@example.com"],
});
firstname: 'Jane',
lastname: 'Smith',
emails: ['jane@example.com']
})
const thunk = getCalendarsListAsync();
const result = await thunk(dispatch, getState, undefined);
const thunk = getCalendarsListAsync()
const result = await thunk(dispatch, getState, undefined)
expect(result.type).toBe("calendars/getCalendars/fulfilled");
expect(result.type).toBe('calendars/getCalendars/fulfilled')
const payload = result.payload as {
importedCalendars: any;
errors: string;
};
importedCalendars: any
errors: string
}
expect(payload.errors).toBe("");
expect(Object.keys(payload.importedCalendars)).toHaveLength(2);
expect(payload.errors).toBe('')
expect(Object.keys(payload.importedCalendars)).toHaveLength(2)
expect(payload.importedCalendars["cal-existing"]).toMatchObject({
id: "cal-existing",
color: { light: "blue", dark: "#FFF" },
events: { "event-1": {} },
});
expect(payload.importedCalendars['cal-existing']).toMatchObject({
id: 'cal-existing',
color: { light: 'blue', dark: '#FFF' },
events: { 'event-1': {} }
})
expect(payload.importedCalendars["cal-new"]).toMatchObject({
id: "cal-new",
name: "New Cal",
owner: { firstname: "Jane", lastname: "Smith" },
});
expect(payload.importedCalendars['cal-new']).toMatchObject({
id: 'cal-new',
name: 'New Cal',
owner: { firstname: 'Jane', lastname: 'Smith' }
})
expect(mockedGetOpenPaasUser).not.toHaveBeenCalled(); // User ID existed in state
expect(mockedGetCalendars).toHaveBeenCalledWith("user-123");
});
expect(mockedGetOpenPaasUser).not.toHaveBeenCalled() // User ID existed in state
expect(mockedGetCalendars).toHaveBeenCalledWith('user-123')
})
it("should fetch user using getOpenPaasUser if openpaasId is not in state", async () => {
getState.mockReturnValue({ calendars: {}, user: {} });
mockedGetOpenPaasUser.mockResolvedValue({ id: "fetched-user-123" });
mockedGetCalendars.mockResolvedValue({ _embedded: { "dav:calendar": [] } });
it('should fetch user using getOpenPaasUser if openpaasId is not in state', async () => {
getState.mockReturnValue({ calendars: {}, user: {} })
mockedGetOpenPaasUser.mockResolvedValue({ id: 'fetched-user-123' })
mockedGetCalendars.mockResolvedValue({ _embedded: { 'dav:calendar': [] } })
const thunk = getCalendarsListAsync();
await thunk(dispatch, getState, undefined);
const thunk = getCalendarsListAsync()
await thunk(dispatch, getState, undefined)
expect(mockedGetOpenPaasUser).toHaveBeenCalled();
expect(mockedGetCalendars).toHaveBeenCalledWith("fetched-user-123");
});
expect(mockedGetOpenPaasUser).toHaveBeenCalled()
expect(mockedGetCalendars).toHaveBeenCalledWith('fetched-user-123')
})
it("should handle error when API call fails", async () => {
getState.mockReturnValue({ calendars: {}, user: {} });
mockedGetOpenPaasUser.mockResolvedValue({ id: "fetched-user-123" });
it('should handle error when API call fails', async () => {
getState.mockReturnValue({ calendars: {}, user: {} })
mockedGetOpenPaasUser.mockResolvedValue({ id: 'fetched-user-123' })
mockedGetCalendars.mockRejectedValue({
response: { status: 500 },
message: "Server Error",
});
message: 'Server Error'
})
// toRejectedError is imported from a mocked module; provide the expected return
const { toRejectedError } = jest.requireMock("@/utils/errorUtils") as {
toRejectedError: jest.Mock;
};
const { toRejectedError } = jest.requireMock('@/utils/errorUtils') as {
toRejectedError: jest.Mock
}
toRejectedError.mockReturnValueOnce({
status: 500,
message: "Server Error",
});
message: 'Server Error'
})
const thunk = getCalendarsListAsync();
const result = await thunk(dispatch, getState, undefined);
const thunk = getCalendarsListAsync()
const result = await thunk(dispatch, getState, undefined)
expect(result.type).toBe("calendars/getCalendars/rejected");
expect(result.type).toBe('calendars/getCalendars/rejected')
expect(result.payload).toEqual({
status: 500,
message: "Server Error",
});
});
message: 'Server Error'
})
})
it("should handle error when fetching owner data fails", async () => {
it('should handle error when fetching owner data fails', async () => {
getState.mockReturnValue({
calendars: {},
user: { userData: { openpaasId: "user-123" } },
});
user: { userData: { openpaasId: 'user-123' } }
})
mockedGetCalendars.mockResolvedValue({
_embedded: { "dav:calendar": [{ id: "cal-1" }] },
});
_embedded: { 'dav:calendar': [{ id: 'cal-1' }] }
})
mockedNormalizeCalendar.mockReturnValue({
cal: { "dav:name": "Error Cal" },
id: "cal-1",
ownerId: "error-123",
});
cal: { 'dav:name': 'Error Cal' },
id: 'cal-1',
ownerId: 'error-123'
})
// fetchOwnerData fails
mockedFetchOwnerData.mockRejectedValueOnce(new Error("Network Error"));
mockedFetchOwnerData.mockRejectedValueOnce(new Error('Network Error'))
const thunk = getCalendarsListAsync();
const result = await thunk(dispatch, getState, undefined);
const thunk = getCalendarsListAsync()
const result = await thunk(dispatch, getState, undefined)
const payload = result.payload as any;
expect(mockedFetchOwnerData).toHaveBeenCalledWith("error-123");
expect(payload.importedCalendars["cal-1"].owner).toEqual({
firstname: "",
lastname: "Unknown User",
emails: [],
});
const payload = result.payload as any
expect(mockedFetchOwnerData).toHaveBeenCalledWith('error-123')
expect(payload.importedCalendars['cal-1'].owner).toEqual({
firstname: '',
lastname: 'Unknown User',
emails: []
})
// Errors array should contain the error
expect(payload.errors).toContain("Network Error");
});
expect(payload.errors).toContain('Network Error')
})
it("should return owner data mapping properly (including resource: true)", async () => {
it('should return owner data mapping properly (including resource: true)', async () => {
getState.mockReturnValue({
calendars: {},
user: { userData: { openpaasId: "user-123" } },
});
user: { userData: { openpaasId: 'user-123' } }
})
mockedGetCalendars.mockResolvedValue({
_embedded: { "dav:calendar": [{ id: "cal-1" }] },
});
_embedded: { 'dav:calendar': [{ id: 'cal-1' }] }
})
mockedNormalizeCalendar.mockReturnValue({
cal: { "dav:name": "Resource Cal" },
id: "cal-1",
ownerId: "resource-123",
});
cal: { 'dav:name': 'Resource Cal' },
id: 'cal-1',
ownerId: 'resource-123'
})
// fetchOwnerData succeeds and returns a resource config structure
mockedFetchOwnerData.mockResolvedValueOnce({
firstname: "Creator",
lastname: "User",
firstname: 'Creator',
lastname: 'User',
emails: [],
resource: true,
});
resource: true
})
const thunk = getCalendarsListAsync();
const result = await thunk(dispatch, getState, undefined);
const thunk = getCalendarsListAsync()
const result = await thunk(dispatch, getState, undefined)
const payload = result.payload as any;
expect(mockedFetchOwnerData).toHaveBeenCalledWith("resource-123");
expect(payload.importedCalendars["cal-1"].owner).toEqual({
firstname: "Creator",
lastname: "User",
const payload = result.payload as any
expect(mockedFetchOwnerData).toHaveBeenCalledWith('resource-123')
expect(payload.importedCalendars['cal-1'].owner).toEqual({
firstname: 'Creator',
lastname: 'User',
emails: [],
resource: true,
});
});
});
resource: true
})
})
})
@@ -1,90 +1,90 @@
import { fetchOwnerData } from "@/features/Calendars/services/helpers";
import { getResourceDetails, getUserDetails } from "@/features/User/userAPI";
import { fetchOwnerData } from '@/features/Calendars/services/helpers'
import { getResourceDetails, getUserDetails } from '@/features/User/userAPI'
jest.mock("@/features/User/userAPI");
jest.mock('@/features/User/userAPI')
const mockedGetUserDetails = getUserDetails as jest.Mock;
const mockedGetResourceDetails = getResourceDetails as jest.Mock;
const mockedGetUserDetails = getUserDetails as jest.Mock
const mockedGetResourceDetails = getResourceDetails as jest.Mock
describe("helpers", () => {
describe('helpers', () => {
beforeEach(() => {
jest.clearAllMocks();
});
jest.clearAllMocks()
})
describe("fetchOwnerData", () => {
it("should return user details successfully", async () => {
describe('fetchOwnerData', () => {
it('should return user details successfully', async () => {
const mockUser = {
firstname: "John",
lastname: "Doe",
emails: ["john@example.com"],
};
mockedGetUserDetails.mockResolvedValueOnce(mockUser);
firstname: 'John',
lastname: 'Doe',
emails: ['john@example.com']
}
mockedGetUserDetails.mockResolvedValueOnce(mockUser)
const result = await fetchOwnerData("user-123");
const result = await fetchOwnerData('user-123')
expect(mockedGetUserDetails).toHaveBeenCalledWith("user-123");
expect(mockedGetResourceDetails).not.toHaveBeenCalled();
expect(result).toEqual(mockUser);
});
expect(mockedGetUserDetails).toHaveBeenCalledWith('user-123')
expect(mockedGetResourceDetails).not.toHaveBeenCalled()
expect(result).toEqual(mockUser)
})
it("should fetch resource details and its creator when user is not found", async () => {
const mockResource = { creator: "creator-456" };
it('should fetch resource details and its creator when user is not found', async () => {
const mockResource = { creator: 'creator-456' }
const mockCreator = {
firstname: "Creator",
lastname: "User",
emails: ["creator@example.com"],
};
firstname: 'Creator',
lastname: 'User',
emails: ['creator@example.com']
}
// Mock getUserDetails to fail with 404 for the initial call
mockedGetUserDetails.mockRejectedValueOnce({
response: { status: 404 },
});
response: { status: 404 }
})
// Mock getResourceDetails to succeed and return a creator ID
mockedGetResourceDetails.mockResolvedValueOnce(mockResource);
mockedGetResourceDetails.mockResolvedValueOnce(mockResource)
// Mock getUserDetails to succeed when called for the creator
mockedGetUserDetails.mockResolvedValueOnce(mockCreator);
mockedGetUserDetails.mockResolvedValueOnce(mockCreator)
const result = await fetchOwnerData("resource-123");
const result = await fetchOwnerData('resource-123')
expect(mockedGetUserDetails).toHaveBeenNthCalledWith(1, "resource-123");
expect(mockedGetResourceDetails).toHaveBeenCalledWith("resource-123");
expect(mockedGetUserDetails).toHaveBeenNthCalledWith(2, "creator-456");
expect(mockedGetUserDetails).toHaveBeenNthCalledWith(1, 'resource-123')
expect(mockedGetResourceDetails).toHaveBeenCalledWith('resource-123')
expect(mockedGetUserDetails).toHaveBeenNthCalledWith(2, 'creator-456')
expect(result).toEqual({
...mockCreator,
resource: true,
administrators: undefined,
resourceIcon: undefined,
});
});
resourceIcon: undefined
})
})
it("should throw error when getUserDetails fails with non-404 error", async () => {
const mockError = { response: { status: 500 } };
mockedGetUserDetails.mockRejectedValueOnce(mockError);
it('should throw error when getUserDetails fails with non-404 error', async () => {
const mockError = { response: { status: 500 } }
mockedGetUserDetails.mockRejectedValueOnce(mockError)
await expect(fetchOwnerData("user-123")).rejects.toEqual(mockError);
await expect(fetchOwnerData('user-123')).rejects.toEqual(mockError)
expect(mockedGetUserDetails).toHaveBeenCalledWith("user-123");
expect(mockedGetResourceDetails).not.toHaveBeenCalled();
});
expect(mockedGetUserDetails).toHaveBeenCalledWith('user-123')
expect(mockedGetResourceDetails).not.toHaveBeenCalled()
})
it("should throw error when getResourceDetails fails", async () => {
const mockError = new Error("Resource not found");
it('should throw error when getResourceDetails fails', async () => {
const mockError = new Error('Resource not found')
// Mock getUserDetails to fail with 404 for the initial call
mockedGetUserDetails.mockRejectedValueOnce({
response: { status: 404 },
});
response: { status: 404 }
})
// Mock getResourceDetails to fail
mockedGetResourceDetails.mockRejectedValueOnce(mockError);
mockedGetResourceDetails.mockRejectedValueOnce(mockError)
await expect(fetchOwnerData("resource-123")).rejects.toEqual(mockError);
await expect(fetchOwnerData('resource-123')).rejects.toEqual(mockError)
expect(mockedGetUserDetails).toHaveBeenCalledWith("resource-123");
expect(mockedGetResourceDetails).toHaveBeenCalledWith("resource-123");
expect(mockedGetUserDetails).toHaveBeenCalledTimes(1); // Only called once
});
});
});
expect(mockedGetUserDetails).toHaveBeenCalledWith('resource-123')
expect(mockedGetResourceDetails).toHaveBeenCalledWith('resource-123')
expect(mockedGetUserDetails).toHaveBeenCalledTimes(1) // Only called once
})
})
})
File diff suppressed because it is too large Load Diff