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: Refactor Code #89

Merged
merged 1 commit into from
Apr 16, 2024
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
4 changes: 1 addition & 3 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ if (!window.Intl) {
})
.then(() => Promise.all([import('intl/locale-data/jsonp/en.js')]))
.then(() => AppRegistry.registerComponent(appName, () => App))
.catch(err => {
throw err;
});
.catch(alert);
} else {
AppRegistry.registerComponent(appName, () => App);
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,48 @@
import React from 'react';
import { Text } from 'react-native';
import { render } from '@testing-library/react-native';

import If from '../index';

describe('<If />', () => {
it('Should render and match the snapshot', () => {
const baseElement = render(<If />);
expect(baseElement).toMatchSnapshot();
});
it('Should render the correct prop component based on the condition', () => {
const conditionTrueText = 'Shoudld render when condition is true';

it('If should render the child component when props.condition = true', () => {
const conditionTrueText = 'Should render when condition is true';
const conditionFalseText = 'Should render condition is false';
const OtherwiseComponent = () => <Text>{conditionFalseText}</Text>;
const TrueConditionComponent = () => <Text>{conditionTrueText}</Text>;
const props = {
otherwise: <OtherwiseComponent />,
condition: true
};
const { getByText } = render(
const { queryByText, getByText } = render(
<If {...props}>
<TrueConditionComponent />
</If>
);
expect(getByText(conditionTrueText)).toBeTruthy();
props.condition = false;
const { getByText: textQueryOnReRender } = render(
expect(queryByText(conditionFalseText)).not.toBeTruthy();
});

it('If should render the component passed on otherwise when props.condition = false', () => {
const conditionFalseText = 'Should render condition is false';
const conditionTrueText = 'Should render when condition is true';
const TrueConditionComponent = () => <Text>{conditionTrueText}</Text>;
const OtherwiseComponent = () => <Text>{conditionFalseText}</Text>;
const props = {
otherwise: <OtherwiseComponent />,
condition: false
};

const { queryByText, getByText } = render(
<If {...props}>
<TrueConditionComponent />
</If>
);
expect(textQueryOnReRender(conditionFalseText)).toBeTruthy();
expect(getByText(conditionFalseText)).toBeTruthy();
expect(queryByText(conditionTrueText)).not.toBeTruthy();
});
});
4 changes: 2 additions & 2 deletions app/components/molecules/CharacterWithQuote/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import styled from 'styled-components/native';
import { get } from 'lodash';
import get from 'lodash/get';
import PropTypes from 'prop-types';
import { fonts } from '@themes';
import T from '@atoms/T';
Expand All @@ -23,7 +23,7 @@ function CharacterWithQuote({ user }) {
<Result
id="wednesday_lover"
values={{
username: get(user, 'character') || 'character'
username: get(user, 'character', 'character')
}}
/>
<Result id="because" />
Expand Down
6 changes: 3 additions & 3 deletions app/components/organisms/SimpsonsLoveWednesday/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components/native';
import { fonts } from '@themes';
import If from '@molecules/If';
import If from '@app/components/atoms/If';
import CharacterWithQuote from '@molecules/CharacterWithQuote';
import LogoWithInstructions from '@molecules/LogoWithInstructions';

const Error = styled.Text`
const Err = styled.Text`
${fonts.style.standard()};
text-align: center;
margin-bottom: 5px;
Expand All @@ -32,7 +32,7 @@ function SimpsonsLoveWednesday({ instructions, user, userErrorMessage }) {
<LogoWithInstructions instructions={instructions} />
<If
condition={!userErrorMessage}
otherwise={<Error>{userErrorMessage}</Error>}
otherwise={<Err>{userErrorMessage}</Err>}
>
<SeparatedView>
<CharacterWithQuote user={user} />
Expand Down
14 changes: 12 additions & 2 deletions app/components/organisms/SimpsonsLoveWednesday/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import React from 'react';
import { renderWithIntl } from '@utils/testUtils';
import { rerender } from '@testing-library/react-native';
import SimpsonsLoveWednesday from '../index';

describe('<SimpsonsLoveWednesday />', () => {
it('Should render and match the snapshot', () => {
const baseElement = renderWithIntl(<SimpsonsLoveWednesday />);
Expand All @@ -29,7 +28,18 @@ describe('<SimpsonsLoveWednesday />', () => {
};
const { getByText } = renderWithIntl(<SimpsonsLoveWednesday {...props} />);
expect(getByText(props.userErrorMessage)).toBeTruthy();
props.userErrorMessage = null;
});
it('Should render the component if userErrorMessage is empty', () => {
const props = {
userErrorMessage: null,
instructions: 'PRESS CMD + D for iOS',
user: {
character: 'Homer',
image:
'https://www.onthisday.com/images/people/homer-simpson-medium.jpg',
quote: "D'Oh!"
}
};
const { getByText: textQueryOnReRender } = renderWithIntl(
<SimpsonsLoveWednesday {...props} />,
rerender
Expand Down
23 changes: 16 additions & 7 deletions app/utils/apiUtils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { create } from 'apisauce';
import mapKeysDeep from 'map-keys-deep';
import { camelCase, snakeCase } from 'lodash';
import camelCase from 'lodash/camelCase';
import snakeCase from 'lodash/snakeCase';
import { Config } from '@app/config/index';

import get from 'lodash/get';
export const apiClients = {
configApi: null,
default: null
Expand All @@ -11,10 +12,14 @@
export const generateApiClient = (type = 'configApi') => {
switch (type) {
case 'configApi':
apiClients[type] = createApiClientWithTransForm(Config.API_URL);
return apiClients[type];
Object.assign(apiClients, {
[type]: createApiClientWithTransForm(Config.API_URL)
});
return get(apiClients, type);
default:
apiClients.default = createApiClientWithTransForm(Config.API_URL);
Object.assign(apiClients, {
default: createApiClientWithTransForm(Config.API_URL)
});

Check warning on line 22 in app/utils/apiUtils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return apiClients.default;

Check warning on line 23 in app/utils/apiUtils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}
};
Expand All @@ -27,15 +32,19 @@
api.addResponseTransform(response => {
const { ok, data } = response;
if (ok && data) {
response.data = mapKeysDeep(data, keys => camelCase(keys));
Object.assign(response, {
data: mapKeysDeep(data, keys => camelCase(keys))
});
}
return response;
});

api.addRequestTransform(request => {
const { data } = request;
if (data) {
request.data = mapKeysDeep(data, keys => snakeCase(keys));
Object.assign(request, {
data: mapKeysDeep(data, keys => snakeCase(keys))

Check warning on line 46 in app/utils/apiUtils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 46 in app/utils/apiUtils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
});

Check warning on line 47 in app/utils/apiUtils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 48 in app/utils/apiUtils.js

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return request;
});
Expand Down
8 changes: 2 additions & 6 deletions app/utils/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ const persistConfig = {
};

export default (rootReducer, rootSaga) => {
const middleware = [];
const enhancers = [];

// Connect the sagas to the redux store
const sagaMiddleware = createSagaMiddleware();
middleware.push(sagaMiddleware);

enhancers.push(applyMiddleware(...middleware));
const middleware = [sagaMiddleware];
const enhancers = [applyMiddleware(...middleware)];

// Redux persist
const persistedReducer = persistReducer(persistConfig, rootReducer);
Expand Down
4 changes: 2 additions & 2 deletions app/utils/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Provider } from 'react-redux';
import createStore from 'app/rootReducer';
import { DEFAULT_LOCALE, translationMessages } from '@app/i18n';
import ConnectedLanguageProvider from '@atoms/LanguageProvider';

import get from 'lodash/get';
export const apiResponseGenerator = (ok, data) => ({
ok,
data
Expand All @@ -14,7 +14,7 @@ export const renderWithIntl = (children, renderFunction = render) =>
renderFunction(
<IntlProvider
locale={DEFAULT_LOCALE}
messages={translationMessages[DEFAULT_LOCALE]}
messages={get(translationMessages, DEFAULT_LOCALE)}
>
{children}
</IntlProvider>
Expand Down
4 changes: 2 additions & 2 deletions setupTests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';

import { LogBox } from 'react-native';
jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);

jest.mock('redux-persist', () => {
Expand All @@ -10,4 +10,4 @@ jest.mock('redux-persist', () => {
};
});

console.disableYellowBox = true;
LogBox.ignoreAllLogs();
Loading