Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
daadaadaah committed Jun 29, 2021
1 parent 64e7a53 commit 437643b
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 17 deletions.
2 changes: 1 addition & 1 deletion assets/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"manifest_version": 2,
"name": "develinky",
"description": "This is web clipper for devlink website",
"description": "This is web clipper for devlinky website",
"version": "1.0.0",
"icons": {
"128": "icon.png"
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = {
statements: 100,
},
},
modulePathIgnorePatterns: ['./src/services/api'],
coveragePathIgnorePatterns: ['./src/services/api'],
modulePathIgnorePatterns: ['./src/services/api', './src/services/chrome'],
coveragePathIgnorePatterns: ['./src/services/api', './src/services/chrome'],
};
19 changes: 6 additions & 13 deletions src/pages/MainPage.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global chrome */
import React from 'react';

import {
Expand All @@ -9,7 +8,7 @@ import { useDispatch, useSelector } from 'react-redux';

import useCurrentUser from '../hooks/useCurrentUser';

import { setUrl, fetchPreview } from '../redux/slice';
import { setUrl, fetchPreview, loadUrl } from '../redux/slice';

import { isEmpty, get } from '../utils';

Expand All @@ -27,15 +26,7 @@ export default function MainPage() {
const url = useSelector(get('url'));

if (!url) {
if (process.env.NODE_ENV !== 'production') {
setTimeout(() => { // 테스트용 코드
dispatch(setUrl('https://jeonghwan-kim.github.io/series/2019/12/10/frontend-dev-env-webpack-basic.html'));
}, 1000);
} else {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
dispatch(setUrl(tabs[0].url));
});
}
dispatch(loadUrl());
}

const preview = useSelector(get('preview'));
Expand All @@ -49,7 +40,7 @@ export default function MainPage() {
};

const handleClick = () => {

dispatch(fetchPreview());
};

return (
Expand Down Expand Up @@ -80,7 +71,9 @@ export default function MainPage() {
value={url || ''}
onChange={handleChange}
/>
<button type="button" aria-label="devlink-url" onClick={handleClick}><i className="fa fa-search" /></button>
<button type="button" id="search-url" aria-label="search-url" onClick={handleClick}>
<i className="fa fa-search" />
</button>
</fieldset>
<>
<h3>preview</h3>
Expand Down
25 changes: 25 additions & 0 deletions src/pages/MainPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,29 @@ describe('<MainPage />', () => {
expect(dispatch).toBeCalledWith(setUrl(newUrl));
});
});

context('when search button is clicked', () => {
const dispatch = jest.fn();

beforeEach(() => {
useCurrentUser.mockImplementation(() => ({
currentUser,
}));

useDispatch.mockImplementation(() => dispatch);

useSelector.mockImplementation((selector) => selector({
url: devlink.url,
preview,
}));
});

it('change dispatch', () => {
const { getByLabelText } = render(<MainPage />);

fireEvent.click(getByLabelText('search-url'));

expect(dispatch).toBeCalledTimes(1);
});
});
});
24 changes: 23 additions & 1 deletion src/redux/asyncActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { getDefaultMiddleware } from '@reduxjs/toolkit';

import configureStore from 'redux-mock-store';

import { fetchPreview, setPreview } from './slice';
import {
fetchPreview, setPreview, loadUrl, setUrl,
} from './slice';

import { fetchUrlMetaData } from '../services/api';
import { fetchUrl } from '../services/chrome';

import { preview } from '../../fixtures';

const mockStore = configureStore(getDefaultMiddleware());

jest.mock('../services/api');
jest.mock('../services/chrome');

describe('actions', () => {
let store;
Expand All @@ -32,4 +36,22 @@ describe('actions', () => {
expect(actions[0]).toEqual(setPreview(preview));
});
});

describe('loadUrl', () => {
beforeEach(() => {
store = mockStore({
url: null,
});

fetchUrl.mockImplementation((callback) => callback(preview.url));
});

it('runs setUrl', async () => {
await store.dispatch(loadUrl());

const actions = store.getActions();

expect(actions[0]).toStrictEqual(setUrl(preview.url));
});
});
});
5 changes: 5 additions & 0 deletions src/redux/slice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSlice } from '@reduxjs/toolkit';

import { fetchUrlMetaData } from '../services/api';
import { fetchUrl } from '../services/chrome';

const { actions, reducer } = createSlice({
name: 'devlinky#',
Expand Down Expand Up @@ -37,6 +38,10 @@ export const {
setPreview,
} = actions;

export const loadUrl = () => (dispatch) => {
fetchUrl((data) => dispatch(setUrl(data)));
};

export const fetchPreview = () => async (dispatch, getState) => {
const { url } = getState();

Expand Down
3 changes: 3 additions & 0 deletions src/services/chrome/__mocks__/chrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const fetchUrl = jest.fn();

export const temp = jest.fn();
15 changes: 15 additions & 0 deletions src/services/chrome/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* global chrome */
export const fetchUrl = (callback) => {
if (process.env.NODE_ENV === 'production') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
callback(tabs[0].url);
});
} else {
const currentTabUrl = 'https://jeonghwan-kim.github.io/series/2019/12/10/frontend-dev-env-webpack-basic.html';
setTimeout(() => {
callback(currentTabUrl);
}, 1000);
}
};

export const temp = () => {};
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = (env, argv) => ({
{ from: './assets/manifest.json', to: './manifest.json' },
{ from: './assets/icon.png', to: './icon.png' },
{ from: './assets/images/logo-small.png', to: './assets/images/logo-small.png' },
{ from: './assets/images/preview_default.png', to: './assets/images/preview_default.png' },
],
}),
],
Expand Down

0 comments on commit 437643b

Please sign in to comment.