Skip to content

Commit

Permalink
Merge pull request #65 from kolorfilm/refactoring/general-refactoring
Browse files Browse the repository at this point in the history
General Refactoring: Change eslint + prettier rules
  • Loading branch information
kolorfilm authored Dec 1, 2024
2 parents 58244d1 + e7f062e commit 1d455bc
Show file tree
Hide file tree
Showing 28 changed files with 403 additions and 412 deletions.
9 changes: 6 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"printWidth": 80,
"semi": false,
"semi": true,
"tabWidth": 2,
"printWidth": 100,
"singleQuote": true,
"trailingComma": "es5"
"trailingComma": "es5",
"bracketSameLine": false,
"bracketSpacing": true
}
38 changes: 28 additions & 10 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import js from '@eslint/js'
import eslint from '@eslint/js'
import tseslint from 'typescript-eslint'
import globals from 'globals'
import nextPlugin from '@next/eslint-plugin-next'
import reactPlugin from 'eslint-plugin-react'
import reactHooksPlugin from 'eslint-plugin-react-hooks'
import jestPlugin from 'eslint-plugin-jest'
import eslintConfigPrettier from 'eslint-config-prettier'
import js from '@eslint/js';
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import globals from 'globals';
import nextPlugin from '@next/eslint-plugin-next';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import importPlugin from 'eslint-plugin-import';
import jestPlugin from 'eslint-plugin-jest';
import eslintConfigPrettier from 'eslint-config-prettier';

export default tseslint.config(
{
Expand All @@ -30,6 +31,7 @@ export default tseslint.config(
'@next/next': nextPlugin,
react: reactPlugin,
'react-hooks': reactHooksPlugin,
import: importPlugin,
jest: jestPlugin,
},
settings: {
Expand All @@ -43,6 +45,22 @@ export default tseslint.config(
rules: {
...nextPlugin.configs.recommended.rules,
...reactHooksPlugin.configs.recommended.rules,
'no-undef': 'error',
curly: 'error',
'object-curly-spacing': ['error', 'always'],
'object-shorthand': ['warn', 'properties'],
'@typescript-eslint/no-unused-vars': ['error'],
'@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }],
'react/react-in-jsx-scope': 'off',
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
'import/namespace': 'warn',
'import/order': [
'error',
{
groups: ['builtin', 'external', ['sibling', 'parent'], 'index'],
},
],
},
},
{
Expand All @@ -52,4 +70,4 @@ export default tseslint.config(
},
},
{ ignores: ['**/public/**', '**/.next/**', '**/coverage/**'] }
)
);
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ module.exports = {
},
],
},
}
};
17 changes: 6 additions & 11 deletions mocks/ordersMapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export const ordersMapped = {
release: {
id: 822281,
resource_url: 'https://api.discogs.com/releases/822281',
description:
'Cassino & Laben* Feat. Marina Puglisi - Lasting Memories (12")',
description: 'Cassino & Laben* Feat. Marina Puglisi - Lasting Memories (12")',
thumbnail:
'https://i.discogs.com/qux7GkBDmaT7KL2J-hhrFbICz8URzFozI3Jm5K5paLk/rs:fit/g:sm/q:40/h:300/w:300/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTgyMjI4/MS0xMjExMTI3NjU5/LmpwZWc.jpeg',
},
Expand Down Expand Up @@ -199,8 +198,7 @@ export const ordersMapped = {
release: {
id: 299663,
resource_url: 'https://api.discogs.com/releases/299663',
description:
'Subsky / Muzo* - Let It Flow / Particular People (12")',
description: 'Subsky / Muzo* - Let It Flow / Particular People (12")',
thumbnail:
'https://i.discogs.com/b71_EoP2pH5bftgCPkpMpRSpvf0OKBaidjM3jGAPBx4/rs:fit/g:sm/q:40/h:300/w:300/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTI5OTY2/My0xNjQ1NzE5NDU1/LTc5MjMuanBlZw.jpeg',
},
Expand Down Expand Up @@ -257,8 +255,7 @@ export const ordersMapped = {
release: {
id: 651168,
resource_url: 'https://api.discogs.com/releases/651168',
description:
'Various - Back To Back Sessions Vol.1 (Sampler) (Disc One) (12", Smplr)',
description: 'Various - Back To Back Sessions Vol.1 (Sampler) (Disc One) (12", Smplr)',
thumbnail:
'https://i.discogs.com/scG4zTNhfy8IwHRueSIp_6irNS6Xa3U_-9pZhsBGKs4/rs:fit/g:sm/q:40/h:300/w:300/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTY1MTE2/OC0xMjM2MjY5OTcw/LmpwZWc.jpeg',
},
Expand All @@ -277,8 +274,7 @@ export const ordersMapped = {
release: {
id: 520920,
resource_url: 'https://api.discogs.com/releases/520920',
description:
'Hawaii / Plastique Vision - Setting Up / Barely Away (12")',
description: 'Hawaii / Plastique Vision - Setting Up / Barely Away (12")',
thumbnail:
'https://i.discogs.com/zhreT-Zy2XexGiTpCUnQcUFl2U6tk0bQKesQqz_GCPI/rs:fit/g:sm/q:40/h:300/w:300/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTUyMDky/MC0xMTg5MDE0MDY2/LmpwZWc.jpeg',
},
Expand Down Expand Up @@ -334,8 +330,7 @@ export const ordersMapped = {
release: {
id: 43284,
resource_url: 'https://api.discogs.com/releases/43284',
description:
'Madonna - What It Feels Like For A Girl (12", Single)',
description: 'Madonna - What It Feels Like For A Girl (12", Single)',
thumbnail:
'https://i.discogs.com/xBPWXV8yKimCFqkLmfGZTr-NrhVtsW51aJLpjMXmpTA/rs:fit/g:sm/q:40/h:300/w:300/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTQzMjg0/LTE1ODkxMTI0MzMt/MjU2OS5qcGVn.jpeg',
},
Expand All @@ -350,4 +345,4 @@ export const ordersMapped = {
itemsSold: 100,
totalAmount: 100,
username: 'testuser',
}
};
106 changes: 51 additions & 55 deletions modules/Orders.test.tsx
Original file line number Diff line number Diff line change
@@ -1,77 +1,73 @@
import { act } from 'react'
import { render, screen } from '@testing-library/react'
import fetchMock from 'jest-fetch-mock'
import Orders from './Orders'
import { ordersMapped } from '../mocks/ordersMapped'
import { ORDER_SUMMARY_LABELS } from './OrdersSummary'
import { act } from 'react';
import { render, screen } from '@testing-library/react';
import fetchMock from 'jest-fetch-mock';
import Orders from './Orders';
import { ordersMapped } from '../mocks/ordersMapped';
import { ORDER_SUMMARY_LABELS } from './OrdersSummary';

jest.mock('highcharts/highstock')
jest.mock('highcharts/highstock');

describe('Orders', () => {
const renderComponent = async (): Promise<void> => {
await act(async () => {
render(<Orders />)
})
}
render(<Orders />);
});
};

beforeEach(() => {
jest.clearAllMocks()
fetchMock.mockResponse(JSON.stringify({ orders: ordersMapped }))
})
jest.clearAllMocks();
fetchMock.mockResponse(JSON.stringify({ orders: ordersMapped }));
});

it('fetches orders correctly', async () => {
await renderComponent()
await renderComponent();

expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledTimes(1);

expect(fetchMock.mock.calls[0][0]).toEqual(
process.env.DISCOGS_APP_URL + '/orders'
)
expect(fetchMock.mock.calls[0][0]).toEqual(process.env.DISCOGS_APP_URL + '/orders');

expect(screen.queryByTestId('orders-loader')).not.toBeInTheDocument()
})
expect(screen.queryByTestId('orders-loader')).not.toBeInTheDocument();
});

it('renders summary correctly', async () => {
await renderComponent()
await renderComponent();

expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledTimes(1);

const profileLink = screen.getByTestId('order-summary-profile-link')
const profileLink = screen.getByTestId('order-summary-profile-link');
expect(profileLink).toHaveAttribute(
'href',
`https://www.discogs.com/de/seller/${ordersMapped.username}/profile`
)
expect(profileLink).toHaveTextContent('Profile Link')
);
expect(profileLink).toHaveTextContent('Profile Link');

ORDER_SUMMARY_LABELS.forEach((label, index) => {
expect(
screen.getByTestId(`order-summary-label-${index}`)
).toHaveTextContent(label)
})
expect(screen.getByTestId(`order-summary-label-${index}`)).toHaveTextContent(label);
});

expect(
screen.getByText(ORDER_SUMMARY_LABELS[0] + ':').parentNode
).toHaveTextContent('2018-09-05')
expect(
screen.getByText(ORDER_SUMMARY_LABELS[1] + ':').parentNode
).toHaveTextContent('2019-02-02')
expect(
screen.getByText(ORDER_SUMMARY_LABELS[2] + ':').parentNode
).toHaveTextContent(ordersMapped.ordersTotal.toString())
expect(
screen.getByText(ORDER_SUMMARY_LABELS[3] + ':').parentNode
).toHaveTextContent(ordersMapped.itemsSold.toString())
expect(
screen.getByText(ORDER_SUMMARY_LABELS[4] + ':').parentNode
).toHaveTextContent(ordersMapped.ordersSent.toString())
expect(
screen.getByText(ORDER_SUMMARY_LABELS[5] + ':').parentNode
).toHaveTextContent(ordersMapped.ordersCancelled.toString())
expect(
screen.getByText(ORDER_SUMMARY_LABELS[6] + ':').parentNode
).toHaveTextContent(ordersMapped.ordersRefund.toString())
expect(
screen.getByText(ORDER_SUMMARY_LABELS[7] + ':').parentNode
).toHaveTextContent(Number(ordersMapped.totalAmount).toFixed(2) + ' €')
})
})
expect(screen.getByText(ORDER_SUMMARY_LABELS[0] + ':').parentNode).toHaveTextContent(
'2018-09-05'
);
expect(screen.getByText(ORDER_SUMMARY_LABELS[1] + ':').parentNode).toHaveTextContent(
'2019-02-02'
);
expect(screen.getByText(ORDER_SUMMARY_LABELS[2] + ':').parentNode).toHaveTextContent(
ordersMapped.ordersTotal.toString()
);
expect(screen.getByText(ORDER_SUMMARY_LABELS[3] + ':').parentNode).toHaveTextContent(
ordersMapped.itemsSold.toString()
);
expect(screen.getByText(ORDER_SUMMARY_LABELS[4] + ':').parentNode).toHaveTextContent(
ordersMapped.ordersSent.toString()
);
expect(screen.getByText(ORDER_SUMMARY_LABELS[5] + ':').parentNode).toHaveTextContent(
ordersMapped.ordersCancelled.toString()
);
expect(screen.getByText(ORDER_SUMMARY_LABELS[6] + ':').parentNode).toHaveTextContent(
ordersMapped.ordersRefund.toString()
);
expect(screen.getByText(ORDER_SUMMARY_LABELS[7] + ':').parentNode).toHaveTextContent(
Number(ordersMapped.totalAmount).toFixed(2) + ' €'
);
});
});
92 changes: 45 additions & 47 deletions modules/Orders.tsx
Original file line number Diff line number Diff line change
@@ -1,80 +1,78 @@
import React, { useEffect, useState } from 'react'
import Head from 'next/head'
import dynamic from 'next/dynamic'
import { useEffect, useState } from 'react';
import dynamic from 'next/dynamic';
import Head from 'next/head';
import OrdersSummary from './OrdersSummary';
import { OrdersMapped } from '../types/OrdersMapped';
import styles from './Orders.module.scss';

const OrdersHighcharts = dynamic(() => import('./OrdersHighcharts'), {
ssr: false,
})
import OrdersSummary from './OrdersSummary'
import { OrdersMapped } from '../types/OrdersMapped'
import styles from './Orders.module.scss'
});

const Orders: React.FC = () => {
const [loading, setLoading] = useState(true)
const [orders, setOrders] = useState<OrdersMapped | null>(null)
const [firstOrderDate, setFirstOrderDate] = useState('')
const [lastOrderDate, setLastOrderDate] = useState('')
const Orders = () => {
const [loading, setLoading] = useState(true);
const [orders, setOrders] = useState<OrdersMapped | undefined>(undefined);
const [firstOrderDate, setFirstOrderDate] = useState('');
const [lastOrderDate, setLastOrderDate] = useState('');

useEffect(() => {
const fetchOrders = async (): Promise<void> => {
fetch(process.env.DISCOGS_APP_URL + '/orders')
.then((orders) => {
return orders.json()
})
.then((orders) => orders.json())
.then((data) => {
if (data.hasAuthError) {
location.assign('authorize')
return
location.assign('authorize');
return;
}

const orders = data.orders
const seriesItems = orders.seriesItems
const orders = data.orders;
const seriesItems = orders.seriesItems;
const formatter = new Intl.DateTimeFormat('en-CA', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
})
const firstOrderDate = formatter.format(seriesItems[0].created)
const lastOrderDate = formatter.format(
seriesItems[seriesItems.length - 1].created
)
});
const firstOrderDate = formatter.format(seriesItems[0].created);
const lastOrderDate = formatter.format(seriesItems[seriesItems.length - 1].created);

setOrders(orders)
setFirstOrderDate(firstOrderDate)
setLastOrderDate(lastOrderDate)
})
}
setOrders(orders);
setFirstOrderDate(firstOrderDate);
setLastOrderDate(lastOrderDate);
});
};

fetchOrders().then()
}, [])
fetchOrders().then();
}, []);

useEffect(() => {
if (orders) {
setLoading(false)
setLoading(false);
}
}, [orders])
}, [orders]);

return (
<div className={styles.container}>
<Head>
<title>Discogs Visualizer</title>
</Head>
<main className={styles.main}>
{loading && (
{loading ? (
<div className={styles.loader} data-testid="orders-loader"></div>
)}
{orders && (
<>
<OrdersHighcharts orders={orders} />
<OrdersSummary
orders={orders}
firstOrderDate={firstOrderDate}
lastOrderDate={lastOrderDate}
/>
</>
) : (
orders && (
<>
<OrdersHighcharts orders={orders} />
<OrdersSummary
orders={orders}
firstOrderDate={firstOrderDate}
lastOrderDate={lastOrderDate}
/>
</>
)
)}
</main>
</div>
)
}
);
};

export default Orders
export default Orders;
Loading

0 comments on commit 1d455bc

Please sign in to comment.