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

Generator creates a project with a singe-locale #325

Merged
merged 27 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2c788e5
Single locale en-US and single site RefArch
adamraya Jan 14, 2022
68bffc4
Build en-US translations
adamraya Jan 14, 2022
1adaba1
Set pwa-kit config url locale to none
adamraya Jan 14, 2022
58b5ea7
Update create-mobify-app script wip
adamraya Jan 14, 2022
c4f7f89
debug create-mobify-app script
adamraya Jan 17, 2022
1db75c1
Use deepmerge to add single locale data
adamraya Jan 18, 2022
51c30e5
Revert test changes in the pwa
adamraya Jan 18, 2022
89b88be
Set locale to none in PWAKit Config data & Fix tests
adamraya Jan 18, 2022
1984967
Fix PWA Kit config json file path
adamraya Jan 18, 2022
bb319c9
Fix test wip
adamraya Jan 18, 2022
fc7a109
More fixing test wip
adamraya Jan 18, 2022
be721ca
Fix locale type in order test URLs
adamraya Jan 19, 2022
8d73a8e
Fix locale type in account tests URLs
adamraya Jan 19, 2022
fffdaba
Fix more locale type in more URLs
adamraya Jan 19, 2022
2ead22f
lint
adamraya Jan 19, 2022
782d5ab
Refactor adding tests utils getPathname
adamraya Jan 19, 2022
1c48384
lint
adamraya Jan 19, 2022
e0387be
Merge branch 'develop' into generator-single-site-singe-locale
adamraya Jan 19, 2022
4e9f7c6
Update test use-auth-modal create an account test condition
adamraya Jan 20, 2022
0073095
Update index.test.js
adamraya Jan 20, 2022
060e2cf
Extract pwa-kit.config.json to assets folder
adamraya Jan 21, 2022
cff1447
Extract l10n config to assets folder
adamraya Jan 21, 2022
ffbc008
Update locale.test.js
adamraya Jan 21, 2022
27383bd
Merge branch 'develop' into generator-single-site-singe-locale
adamraya Jan 21, 2022
3846abd
Merge branch 'develop' into generator-single-site-singe-locale
adamraya Jan 25, 2022
0b4a460
Merge branch 'develop' into generator-single-site-singe-locale
adamraya Jan 25, 2022
5a9fbae
Update CHANGELOG.md
adamraya Jan 25, 2022
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
28 changes: 26 additions & 2 deletions packages/pwa-kit-create-app/scripts/create-mobify-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ const runGenerator = (answers, {outputDir}) => {

const pkgJsonPath = p.resolve(outputDir, 'package.json')
const pkgJSON = readJson(pkgJsonPath)
const finalPkgData = merge(pkgJSON, answers['scaffold-pwa'])
const pkgDataWithAnswers = merge(pkgJSON, answers['scaffold-pwa'])
const finalPkgData = merge(pkgDataWithAnswers, pkgSingleLocaleData)

npmInstallables.forEach((pkgName) => {
const keys = ['dependencies', 'devDependencies']
Expand Down Expand Up @@ -150,6 +151,9 @@ const runGenerator = (answers, {outputDir}) => {
]
})

const PWAKitConfigJsonPath = p.resolve(outputDir, 'pwa-kit.config.json')
writeJson(PWAKitConfigJsonPath, PWAKitConfigSingleLocaleData)

const APIConfigTemplate = require(`../assets/pwa/api.config`).template
const commerceApi = {
proxyPath: answers['scaffold-pwa'].mobify.ssrParameters.proxyConfigs[0].path,
Expand Down Expand Up @@ -294,12 +298,32 @@ const buildAnswers = ({
}
}

const pkgSingleLocaleData = {
l10n: {
supportedCurrencies: ['USD'],
defaultCurrency: 'USD',
supportedLocales: [
{
id: 'en-US',
preferredCurrency: 'USD'
}
],
defaultLocale: 'en-US'
}
}

const PWAKitConfigSingleLocaleData = {
url: {
locale: 'none'
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about extracting these config-related objects into files in the folder 'packages/pwa-kit-create-app/assets/'? The ticket asks about creating files in that asset folder. It may be better to group all the files together in the same location.

Copy link
Collaborator Author

@adamraya adamraya Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! I didn't know that we added an /assets folder to the generator and I missed that implementation detail.

Done, now the configuration objects are in files inside the assets folder.


const testProjectAnswers = () => {
const config = {
projectId: 'scaffold-pwa',
instanceUrl: 'https://zzrf-001.sandbox.us01.dx.commercecloud.salesforce.com',
clientId: 'c9c45bfd-0ed3-4aa2-9971-40f88962b836',
siteId: 'RefArchGlobal',
siteId: 'RefArch',
organizationId: 'f_ecom_zzrf_001',
shortCode: 'kv7kzm78',
einsteinId: '1ea06c6e-c936-4324-bcf0-fada93f83bb1',
Expand Down
2 changes: 0 additions & 2 deletions packages/pwa/app/components/drawer-menu/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ test('Renders DrawerMenu with root', () => {

const drawer = document.querySelector('.chakra-portal')
const accordion = document.querySelector('.chakra-accordion')
const localeSelector = document.querySelector('.sf-locale-selector')
const socialIcons = document.querySelector('.sf-social-icons')

expect(drawer).toBeInTheDocument()
expect(accordion).toBeInTheDocument()
expect(localeSelector).toBeInTheDocument()
expect(socialIcons).toBeInTheDocument()
})

Expand Down
14 changes: 7 additions & 7 deletions packages/pwa/app/components/header/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import PropTypes from 'prop-types'

import {fireEvent, screen, waitFor} from '@testing-library/react'
import Header from './index'
import {renderWithProviders} from '../../utils/test-utils'
import {renderWithProviders, getPathname} from '../../utils/test-utils'
import useCustomer from '../../commerce-api/hooks/useCustomer'
import {setupServer} from 'msw/node'
import {rest} from 'msw'
Expand Down Expand Up @@ -47,11 +47,11 @@ const MockedComponent = ({history}) => {
const onAccountClick = () => {
// Link to account page for registered customer, open auth modal otherwise
if (customer.isRegistered) {
history.push('/en-GB/account')
history.push(getPathname('/account'))
}
}
const onWishlistClick = () => {
history.push('/en-GB/account/wishlist')
history.push(getPathname('/account/wishlist'))
}

return (
Expand Down Expand Up @@ -112,7 +112,7 @@ beforeEach(() => {

// Since we're testing some navigation logic, we are using a simple Router
// around our component. We need to initialize the default route/path here.
window.history.pushState({}, 'Account', '/en-GB/account')
window.history.pushState({}, 'Account', getPathname('/account'))
})
afterEach(() => {
localStorage.clear()
Expand Down Expand Up @@ -232,12 +232,12 @@ test('route to account page when an authenticated users click on account icon',
const accountIcon = document.querySelector('svg[aria-label="My account"]')
fireEvent.click(accountIcon)
await waitFor(() => {
expect(history.push).toHaveBeenCalledWith('/en-GB/account')
expect(history.push).toHaveBeenCalledWith(getPathname('/account'))
})

fireEvent.keyDown(accountIcon, {key: 'Enter', code: 'Enter'})
await waitFor(() => {
expect(history.push).toHaveBeenCalledWith('/en-GB/account')
expect(history.push).toHaveBeenCalledWith(getPathname('/account'))
})
})

Expand Down Expand Up @@ -283,7 +283,7 @@ test('route to wishlist page when an authenticated users click on wishlist icon'
const wishlistIcon = document.querySelector('button[aria-label="Wishlist"]')
fireEvent.click(wishlistIcon)
await waitFor(() => {
expect(history.push).toHaveBeenCalledWith('/en-GB/account/wishlist')
expect(history.push).toHaveBeenCalledWith(getPathname('/account/wishlist'))
})
})

Expand Down
6 changes: 3 additions & 3 deletions packages/pwa/app/components/image-gallery/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {screen, fireEvent, waitFor} from '@testing-library/react'
import ImageGallery from './index'
import {Skeleton as ImageGallerySkeleton} from './index'
import {createMemoryHistory} from 'history'
import {renderWithProviders} from '../../utils/test-utils'
import {renderWithProviders, getPathname} from '../../utils/test-utils'

const MockComponent = ({imageGroups = [], selectedVariationAttributes = {}}) => {
return !imageGroups.length ? (
Expand All @@ -30,7 +30,7 @@ MockComponent.propTypes = {
describe('Image Gallery Component', () => {
test('renders component with all images', () => {
const history = createMemoryHistory()
history.push('/en-GB/image-gallery')
history.push(getPathname('/image-gallery'))

renderWithProviders(<MockComponent imageGroups={data} selectedVariationAttributes={{}} />)
expect(screen.getAllByAltText(/Ruffle Front V-Neck Cardigan/).length).toEqual(3)
Expand All @@ -43,7 +43,7 @@ describe('Image Gallery Component', () => {

test('can select thumbnail image with enter keyboard', async () => {
const history = createMemoryHistory()
history.push('/en-GB/image-gallery')
history.push(getPathname('/image-gallery'))

renderWithProviders(
<MockComponent imageGroups={data} selectedVariationAttributes={{}} history={history} />
Expand Down
9 changes: 5 additions & 4 deletions packages/pwa/app/components/link/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {renderWithProviders} from '../../utils/test-utils'
import {renderWithProviders, getPathname} from '../../utils/test-utils'
import Link from './index'
import {DEFAULT_LOCALE} from '../../constants'

import {getUrlConfig} from '../../utils/utils'
jest.mock('../../utils/utils', () => {
Expand All @@ -21,23 +22,23 @@ test('renders a link with locale prepended', () => {
locale: 'path'
}))
const {getByText} = renderWithProviders(<Link href="/mypage">My Page</Link>)
expect(getByText(/My Page/i)).toHaveAttribute('href', '/en-GB/mypage')
expect(getByText(/My Page/i)).toHaveAttribute('href', getPathname('/mypage'))
})

test('renders a link with locale as query param', () => {
getUrlConfig.mockImplementation(() => ({
locale: 'query_param'
}))
const {getByText} = renderWithProviders(<Link href="/mypage">My Page</Link>)
expect(getByText(/My Page/i)).toHaveAttribute('href', '/mypage?locale=en-GB')
expect(getByText(/My Page/i)).toHaveAttribute('href', `/mypage?locale=${DEFAULT_LOCALE}`)
})

test('accepts `to` prop as well', () => {
getUrlConfig.mockImplementation(() => ({
locale: 'path'
}))
const {getByText} = renderWithProviders(<Link to="/mypage">My Page</Link>)
expect(getByText(/My Page/i)).toHaveAttribute('href', '/en-GB/mypage')
expect(getByText(/My Page/i)).toHaveAttribute('href', getPathname('/mypage'))
})

test('does not modify root url', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/pwa/app/components/search/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {renderWithProviders} from '../../utils/test-utils'
import {renderWithProviders, getPathname} from '../../utils/test-utils'
import user from '@testing-library/user-event'
import {screen, waitFor} from '@testing-library/react'
import SearchInput from './index'
Expand Down Expand Up @@ -56,7 +56,7 @@ test('changes url when enter is pressed', async () => {
renderWithProviders(<SearchInput />)
const searchInput = document.querySelector('input[type="search"]')
await user.type(searchInput, 'Dresses{enter}')
await waitFor(() => expect(window.location.pathname).toEqual('/en-GB/search'))
await waitFor(() => expect(window.location.pathname).toEqual(getPathname('/search')))
await waitFor(() => expect(window.location.search).toEqual('?q=Dresses'))
})

Expand Down
7 changes: 3 additions & 4 deletions packages/pwa/app/hooks/use-auth-modal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React from 'react'
import {screen, within, waitFor} from '@testing-library/react'
import user from '@testing-library/user-event'
import {renderWithProviders} from '../utils/test-utils'
import {renderWithProviders, getPathname} from '../utils/test-utils'
import {AuthModal, useAuthModal} from './use-auth-modal'
import {BrowserRouter as Router, Route} from 'react-router-dom'
import Account from '../pages/account'
Expand Down Expand Up @@ -125,7 +125,7 @@ const MockedComponent = () => {
<Router>
<button onClick={authModal.onOpen}>Open Modal</button>
<AuthModal {...authModal} />
<Route path="/en-GB/account">
<Route path={getPathname('/account')}>
<Account match={match} />
</Route>
</Router>
Expand Down Expand Up @@ -245,6 +245,5 @@ test('Allows customer to create an account', async () => {
user.click(withinForm.getByText(/create account/i))

// wait for redirecting to account page
expect(await screen.findByText(/customer@test.com/i, {}, {timeout: 30000})).toBeInTheDocument()
expect(await screen.queryAllByText(/Log out/i, {}, {timeout: 30000}).length).toEqual(2)
expect(await screen.findByText(/welcome tester/i, {}, {timeout: 30000})).toBeInTheDocument()
})
28 changes: 8 additions & 20 deletions packages/pwa/app/pages/account/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {screen, waitFor, within} from '@testing-library/react'
import user from '@testing-library/user-event'
import {rest} from 'msw'
import {setupServer} from 'msw/node'
import {renderWithProviders} from '../../utils/test-utils'
import {renderWithProviders, getPathname} from '../../utils/test-utils'
import {
mockedGuestCustomer,
mockedRegisteredCustomer,
Expand All @@ -20,7 +20,6 @@ import {
} from '../../commerce-api/mock-data'
import useCustomer from '../../commerce-api/hooks/useCustomer'
import Account from './index'
import {getUrlConfig} from '../../utils/utils'

jest.mock('../../commerce-api/utils', () => {
const originalModule = jest.requireActual('../../commerce-api/utils')
Expand All @@ -30,14 +29,6 @@ jest.mock('../../commerce-api/utils', () => {
}
})

jest.mock('../../utils/utils', () => {
const original = jest.requireActual('../../utils/utils')
return {
...original,
getUrlConfig: jest.fn()
}
})

const MockedComponent = () => {
const customer = useCustomer()

Expand All @@ -49,7 +40,7 @@ const MockedComponent = () => {

return (
<Switch>
<Route path="/en-GB/account" render={(props) => <Account {...props} />} />
<Route path={getPathname('/account')} render={(props) => <Account {...props} />} />
</Switch>
)
}
Expand Down Expand Up @@ -98,13 +89,10 @@ const server = setupServer(
beforeEach(() => {
jest.resetModules()
server.listen({onUnhandledRequest: 'error'})
getUrlConfig.mockImplementation(() => ({
locale: 'path'
}))

// Since we're testing some navigation logic, we are using a simple Router
// around our component. We need to initialize the default route/path here.
window.history.pushState({}, 'Account', '/en-GB/account')
window.history.pushState({}, 'Account', getPathname('/account'))
})
afterEach(() => {
localStorage.clear()
Expand All @@ -119,7 +107,7 @@ test('Redirects to login page if the customer is not logged in', async () => {
})
)
renderWithProviders(<MockedComponent />)
await waitFor(() => expect(window.location.pathname).toEqual('/en-GB/login'))
await waitFor(() => expect(window.location.pathname).toEqual(getPathname('/login')))
})

test('Provides navigation for subpages', async () => {
Expand All @@ -128,11 +116,11 @@ test('Provides navigation for subpages', async () => {

const nav = within(screen.getByTestId('account-detail-nav'))
user.click(nav.getByText('Addresses'))
await waitFor(() => expect(window.location.pathname).toEqual('/en-GB/account/addresses'))
await waitFor(() => expect(window.location.pathname).toEqual(getPathname('/account/addresses')))
user.click(nav.getByText('Order History'))
await waitFor(() => expect(window.location.pathname).toEqual('/en-GB/account/orders'))
await waitFor(() => expect(window.location.pathname).toEqual(getPathname('/account/orders')))
user.click(nav.getByText('Payment Methods'))
await waitFor(() => expect(window.location.pathname).toEqual('/en-GB/account/payments'))
await waitFor(() => expect(window.location.pathname).toEqual(getPathname('/account/payments')))
})

test('Renders account detail page by default for logged-in customer', async () => {
Expand All @@ -149,7 +137,7 @@ test('Allows customer to sign out', async () => {
expect(await screen.findByTestId('account-detail-page')).toBeInTheDocument()
user.click(screen.getAllByText(/Log Out/)[0])
await waitFor(() => {
expect(window.location.pathname).toEqual('/en-GB/login')
expect(window.location.pathname).toEqual(getPathname('/login'))
})
})

Expand Down
19 changes: 4 additions & 15 deletions packages/pwa/app/pages/account/orders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,14 @@ import {screen} from '@testing-library/react'
import user from '@testing-library/user-event'
import {rest} from 'msw'
import {setupServer} from 'msw/node'
import {renderWithProviders} from '../../utils/test-utils'
import {renderWithProviders, getPathname} from '../../utils/test-utils'
import {
mockedRegisteredCustomer,
mockOrderHistory,
mockOrderProducts
} from '../../commerce-api/mock-data'
import useCustomer from '../../commerce-api/hooks/useCustomer'
import Orders from './orders'
import {getUrlConfig} from '../../utils/utils'

jest.mock('../../utils/utils', () => {
const original = jest.requireActual('../../utils/utils')
return {
...original,
getUrlConfig: jest.fn()
}
})

jest.mock('../../commerce-api/utils', () => {
const originalModule = jest.requireActual('../../commerce-api/utils')
Expand All @@ -51,7 +42,7 @@ const MockedComponent = () => {

return (
<Switch>
<Route path="/en-GB/account/orders">
<Route path={getPathname('/account/orders')}>
<Orders />
</Route>
</Switch>
Expand Down Expand Up @@ -105,12 +96,10 @@ const server = setupServer(
// Set up and clean up
beforeEach(() => {
jest.resetModules()
getUrlConfig.mockImplementation(() => ({
locale: 'path'
}))

server.listen({onUnhandledRequest: 'error'})

window.history.pushState({}, 'Account', '/en-GB/account/orders')
window.history.pushState({}, 'Account', getPathname('/account/orders'))
})
afterEach(() => {
localStorage.clear()
Expand Down
Loading