Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check for github scopes #673

Merged
merged 8 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/components/fields/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface IFieldCheckbox {
checked: boolean;
onChange: any;
placeholder?: string;
disabled?: boolean;
}

export const FieldCheckbox = (props: IFieldCheckbox) => {
Expand All @@ -18,13 +19,17 @@ export const FieldCheckbox = (props: IFieldCheckbox) => {
className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
checked={props.checked}
onChange={props.onChange}
disabled={props.disabled}
/>
</div>

<div className="ml-3 text-sm">
<label
htmlFor={props.name}
className="font-medium text-gray-700 dark:text-gray-200"
style={
props.disabled ? { textDecoration: 'line-through' } : undefined
}
>
{props.label}
</label>
Expand Down
4 changes: 2 additions & 2 deletions src/context/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe('context/App.tsx', () => {
participating: true,
playSound: true,
showNotifications: true,
colors: true,
colors: false,
},
);
});
Expand Down Expand Up @@ -302,7 +302,7 @@ describe('context/App.tsx', () => {
participating: false,
playSound: true,
showNotifications: true,
colors: true,
colors: false,
},
);
});
Expand Down
12 changes: 6 additions & 6 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, {
useState,
createContext,
useCallback,
useEffect,
useMemo,
useState,
} from 'react';

import { useInterval } from '../hooks/useInterval';
import { useNotifications } from '../hooks/useNotifications';
import {
AccountNotifications,
Appearance,
Expand All @@ -15,14 +17,12 @@ import {
SettingsState,
} from '../types';
import { apiRequestAuth } from '../utils/api-requests';
import { addAccount, authGitHub, getToken, getUserData } from '../utils/auth';
import { clearState, loadState, saveState } from '../utils/storage';
import { setAppearance } from '../utils/appearance';
import { addAccount, authGitHub, getToken, getUserData } from '../utils/auth';
import { setAutoLaunch } from '../utils/comms';
import { useInterval } from '../hooks/useInterval';
import { useNotifications } from '../hooks/useNotifications';
import Constants from '../utils/constants';
import { generateGitHubAPIUrl } from '../utils/helpers';
import { clearState, loadState, saveState } from '../utils/storage';

const defaultAccounts: AuthState = {
token: null,
Expand All @@ -37,7 +37,7 @@ export const defaultSettings: SettingsState = {
markOnClick: false,
openAtStartup: false,
appearance: Appearance.SYSTEM,
colors: true,
colors: false,
afonsojramos marked this conversation as resolved.
Show resolved Hide resolved
};

interface AppContextState {
Expand Down
177 changes: 160 additions & 17 deletions src/routes/Settings.test.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import TestRenderer, { act } from 'react-test-renderer';
import { render, fireEvent } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import TestRenderer, { act } from 'react-test-renderer';

const { ipcRenderer } = require('electron');

import { SettingsRoute } from './Settings';
import { AxiosResponse } from 'axios';
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';
import { AppContext } from '../context/App';
import { mockSettings } from '../__mocks__/mock-state';
import * as apiRequests from '../utils/api-requests';
import Constants from '../utils/constants';
import { SettingsRoute } from './Settings';

const mockNavigate = jest.fn();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
}));
jest.spyOn(apiRequests, 'apiRequestAuth').mockResolvedValue({
headers: {
'x-oauth-scopes': Constants.AUTH_SCOPE.join(', '),
},
} as unknown as AxiosResponse);

describe('routes/Settings.tsx', () => {
const updateSetting = jest.fn();

beforeEach(() => {
mockNavigate.mockReset();
updateSetting.mockReset();
jest.clearAllMocks();
});

it('should render itself & its children', async () => {
let tree: TestRenderer;

await act(async () => {
tree = TestRenderer.create(
<AppContext.Provider value={{ settings: mockSettings }}>
<AppContext.Provider
value={{ settings: mockSettings, accounts: mockAccounts }}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -45,7 +54,11 @@ describe('routes/Settings.tsx', () => {
await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider
value={{ settings: mockSettings, logout: logoutMock }}
value={{
settings: mockSettings,
accounts: mockAccounts,
logout: logoutMock,
}}
>
<MemoryRouter>
<SettingsRoute />
Expand All @@ -70,7 +83,12 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -88,7 +106,13 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -110,7 +134,13 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -132,7 +162,13 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -154,7 +190,13 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -176,7 +218,13 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -198,7 +246,13 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings, updateSetting }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -218,7 +272,12 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -238,7 +297,9 @@ describe('routes/Settings.tsx', () => {

await act(async () => {
const { getByLabelText: getByLabelTextLocal } = render(
<AppContext.Provider value={{ settings: mockSettings }}>
<AppContext.Provider
value={{ settings: mockSettings, accounts: mockAccounts }}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
Expand All @@ -250,4 +311,86 @@ describe('routes/Settings.tsx', () => {
fireEvent.click(getByLabelText('Quit Gitify'));
expect(ipcRenderer.send).toHaveBeenCalledWith('app-quit');
});

it('should be able to enable colors', async () => {
jest.spyOn(apiRequests, 'apiRequestAuth').mockResolvedValue({
headers: {
'x-oauth-scopes': Constants.AUTH_SCOPE.join(', '),
},
} as unknown as AxiosResponse);

await act(async () => {
render(
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
});

await screen.findByLabelText('Use GitHub-like state colors');

fireEvent.click(screen.getByLabelText('Use GitHub-like state colors'));

expect(updateSetting).toHaveBeenCalledTimes(1);
expect(updateSetting).toHaveBeenCalledWith('colors', true);
});

it('should not be able to enable colors due to missing scope', async () => {
jest.spyOn(apiRequests, 'apiRequestAuth').mockResolvedValue({
headers: {
'x-oauth-scopes': 'read:user, notifications',
},
} as unknown as AxiosResponse);

await act(async () => {
render(
<AppContext.Provider
value={{
settings: mockSettings,
accounts: mockAccounts,
updateSetting,
}}
>
<MemoryRouter>
<SettingsRoute />
</MemoryRouter>
</AppContext.Provider>,
);
});

expect(
screen
.getByLabelText('Use GitHub-like state colors (requires repo scope)')
.closest('input'),
).toHaveProperty('disabled', true);

// click the checkbox
fireEvent.click(
screen.getByLabelText(
'Use GitHub-like state colors (requires repo scope)',
),
);

// check if the checkbox is still unchecked
expect(updateSetting).not.toHaveBeenCalled();
expect(
screen.getByLabelText(
'Use GitHub-like state colors (requires repo scope)',
),
).not.toBe('checked');

expect(
screen.getByLabelText(
'Use GitHub-like state colors (requires repo scope)',
).parentNode.parentNode,
).toMatchSnapshot();
});
});
Loading