-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove global mocks, localize to test. * Remove more global mocks. * Make dbCommand tests work again. * Write snapshot tests for generators. * Add generate tests and snaps. * Play around with snapshot testing. * Update snapshots. * Complete reorg of generator files and templates, adds text and fixture directories Cell test working * Component, layout, page and service tests * Adds tests for generator helpers.js * More tests * Comment out some variables that aren't being used for now * Adds multi-word cell tests * Variable name change * Adds multi-word component tests * Component test fix * Adds multi-word layout tests * Adds multi-word page tests * Creates multi-word and CRUD sdl tests * Adds multi word tests for service * Adds scaffold tests * Count actual number of files returned * Rename test directories to __tests__ * Adds tests for src/lib/index * Ignore any files named "test" inside a fixtures directory * Adds generateTemplate tests * Ignore fixture files when linting * fix babel build ignore error using rimraf (rm -rf) Babel flag --no-copy-ignored has a bug and does not respect our ignored config. For a short term fix, I'm using rimraf package (cross-platform support) to clean up __tests__ folders that shouldn't be in dist/ * Converts SDL generator to use same sub-generator technique as scaffold generator * Updates README with new generator file structure * Removes .fixture extensions * Remove unused argument Co-authored-by: Rob Cameron <rob.cameron@fastmail.com> Co-authored-by: David S Price <thedavid@thedavidprice.com>
- Loading branch information
1 parent
05bfc65
commit 8d510db
Showing
101 changed files
with
2,329 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
module.exports = { | ||
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/*.test.[jt]s?(x)'], | ||
testPathIgnorePatterns: ['fixtures'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/cli/src/commands/generate/__tests__/helpers.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
global.__dirname = __dirname | ||
import {} from 'src/lib/test' | ||
|
||
import * as helpers from '../helpers' | ||
|
||
const PAGE_TEMPLATE_OUTPUT = `const FooBarPage = () => { | ||
return ( | ||
<div> | ||
<h1>FooBarPage</h1> | ||
<p>Find me in ./web/src/pages/FooBarPage/FooBarPage.js</p> | ||
</div> | ||
) | ||
} | ||
export default FooBarPage | ||
` | ||
|
||
test('templateForComponentFile creates a proper output path for files', () => { | ||
const names = ['FooBar', 'fooBar', 'foo-bar', 'foo_bar', 'FOO_BAR'] | ||
|
||
names.forEach((name) => { | ||
const output = helpers.templateForComponentFile({ | ||
name: name, | ||
suffix: 'Page', | ||
webPathSection: 'pages', | ||
generator: 'page', | ||
templatePath: 'page.js.template', | ||
}) | ||
|
||
expect(output[0]).toEqual( | ||
'/path/to/project/web/src/pages/FooBarPage/FooBarPage.js' | ||
) | ||
}) | ||
}) | ||
|
||
test('templateForComponentFile can create a path in /web', () => { | ||
const output = helpers.templateForComponentFile({ | ||
name: 'Home', | ||
suffix: 'Page', | ||
webPathSection: 'pages', | ||
generator: 'page', | ||
templatePath: 'page.js.template', | ||
}) | ||
|
||
expect(output[0]).toEqual( | ||
'/path/to/project/web/src/pages/HomePage/HomePage.js' | ||
) | ||
}) | ||
|
||
test('templateForComponentFile can create a path in /api', () => { | ||
const output = helpers.templateForComponentFile({ | ||
name: 'Home', | ||
suffix: 'Page', | ||
apiPathSection: 'services', | ||
generator: 'page', | ||
templatePath: 'page.js.template', | ||
}) | ||
|
||
expect(output[0]).toEqual( | ||
'/path/to/project/api/src/services/HomePage/HomePage.js' | ||
) | ||
}) | ||
|
||
test('templateForComponentFile can override generated component name', () => { | ||
const output = helpers.templateForComponentFile({ | ||
name: 'Home', | ||
componentName: 'Hobbiton', | ||
webPathSection: 'pages', | ||
generator: 'page', | ||
templatePath: 'page.js.template', | ||
}) | ||
|
||
expect(output[0]).toEqual( | ||
'/path/to/project/web/src/pages/Hobbiton/Hobbiton.js' | ||
) | ||
}) | ||
|
||
test('templateForComponentFile can override file extension', () => { | ||
const output = helpers.templateForComponentFile({ | ||
name: 'Home', | ||
suffix: 'Page', | ||
extension: '.txt', | ||
webPathSection: 'pages', | ||
generator: 'page', | ||
templatePath: 'page.js.template', | ||
}) | ||
|
||
expect(output[0]).toEqual( | ||
'/path/to/project/web/src/pages/HomePage/HomePage.txt' | ||
) | ||
}) | ||
|
||
test('templateForComponentFile creates a template', () => { | ||
const output = helpers.templateForComponentFile({ | ||
name: 'FooBar', | ||
suffix: 'Page', | ||
webPathSection: 'pages', | ||
generator: 'page', | ||
templatePath: 'page.js.template', | ||
}) | ||
|
||
expect(output[1]).toEqual(PAGE_TEMPLATE_OUTPUT) | ||
}) | ||
|
||
test('pathName uses passed path if present', () => { | ||
const paths = ['FooBar', 'fooBar', 'foo-bar', 'foo_bar', 'foobar', '/foobar'] | ||
|
||
paths.forEach((path) => { | ||
expect(helpers.pathName(path, 'FooBar')).toEqual(path) | ||
}) | ||
}) | ||
|
||
test('pathName creates path based on name if path is null', () => { | ||
const names = ['FooBar', 'fooBar', 'foo-bar', 'foo_bar', 'FOO_BAR'] | ||
|
||
names.forEach((name) => { | ||
expect(helpers.pathName(null, name)).toEqual('/foo-bar') | ||
}) | ||
}) |
45 changes: 45 additions & 0 deletions
45
packages/cli/src/commands/generate/cell/__tests__/cell.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
global.__dirname = __dirname | ||
import { loadGeneratorFixture } from 'src/lib/test' | ||
|
||
import * as cell from '../cell' | ||
|
||
let singleWordFiles, multiWordFiles | ||
|
||
beforeAll(() => { | ||
singleWordFiles = cell.files({ name: 'User' }) | ||
multiWordFiles = cell.files({ name: 'UserProfile' }) | ||
}) | ||
|
||
test('returns exactly 2 files', () => { | ||
expect(Object.keys(singleWordFiles).length).toEqual(2) | ||
}) | ||
|
||
test('creates a cell component with a single word name', () => { | ||
expect( | ||
singleWordFiles['/path/to/project/web/src/components/UserCell/UserCell.js'] | ||
).toEqual(loadGeneratorFixture('cell', 'singleWordCell.js')) | ||
}) | ||
|
||
test('creates a cell test with a single word name', () => { | ||
expect( | ||
singleWordFiles[ | ||
'/path/to/project/web/src/components/UserCell/UserCell.test.js' | ||
] | ||
).toEqual(loadGeneratorFixture('cell', 'singleWordCell.test.js')) | ||
}) | ||
|
||
test('creates a cell component with a multi word name', () => { | ||
expect( | ||
multiWordFiles[ | ||
'/path/to/project/web/src/components/UserProfileCell/UserProfileCell.js' | ||
] | ||
).toEqual(loadGeneratorFixture('cell', 'multiWordCell.js')) | ||
}) | ||
|
||
test('creates a cell test with a multi word name', () => { | ||
expect( | ||
multiWordFiles[ | ||
'/path/to/project/web/src/components/UserProfileCell/UserProfileCell.test.js' | ||
] | ||
).toEqual(loadGeneratorFixture('cell', 'multiWordCell.test.js')) | ||
}) |
17 changes: 17 additions & 0 deletions
17
packages/cli/src/commands/generate/cell/__tests__/fixtures/multiWordCell.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const QUERY = gql` | ||
query { | ||
userProfile { | ||
id | ||
} | ||
} | ||
` | ||
|
||
export const Loading = () => <div>Loading...</div> | ||
|
||
export const Empty = () => <div>Empty</div> | ||
|
||
export const Failure = ({ error }) => <div>Error: {error.message}</div> | ||
|
||
export const Success = ({ userProfile }) => { | ||
return JSON.stringify(userProfile) | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/cli/src/commands/generate/cell/__tests__/fixtures/multiWordCell.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { render, cleanup, screen } from '@testing-library/react' | ||
|
||
import { Loading, Empty, Failure, Success } from './UserProfileCell' | ||
|
||
describe('UserProfileCell', () => { | ||
afterEach(() => { | ||
cleanup() | ||
}) | ||
|
||
it('Loading renders successfully', () => { | ||
render(<Loading />) | ||
// Use screen.debug() to see output. | ||
expect(screen.queryByText('Loading...')).toBeInTheDocument() | ||
}) | ||
|
||
it('Empty renders successfully', () => { | ||
render(<Empty />) | ||
expect(screen.queryByText('Empty')).toBeInTheDocument() | ||
}) | ||
|
||
it('Failure renders successfully', () => { | ||
render(<Failure error={{ message: 'Oh no!' }} />) | ||
expect(screen.queryByText('Error: Oh no!')).toBeInTheDocument() | ||
}) | ||
|
||
it('Success renders successfully', () => { | ||
render( | ||
<Success userExample={{ userProfile: { objectKey: 'objectValue' } }} /> | ||
) | ||
expect( | ||
screen.queryByText('{"userProfile":{"objectKey":"objectValue"}}') | ||
).toBeInTheDocument() | ||
}) | ||
}) |
17 changes: 17 additions & 0 deletions
17
packages/cli/src/commands/generate/cell/__tests__/fixtures/singleWordCell.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const QUERY = gql` | ||
query { | ||
user { | ||
id | ||
} | ||
} | ||
` | ||
|
||
export const Loading = () => <div>Loading...</div> | ||
|
||
export const Empty = () => <div>Empty</div> | ||
|
||
export const Failure = ({ error }) => <div>Error: {error.message}</div> | ||
|
||
export const Success = ({ user }) => { | ||
return JSON.stringify(user) | ||
} |
Oops, something went wrong.