This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add build-in Jest for testing (#38)
- Loading branch information
Showing
29 changed files
with
541 additions
and
32 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Do this as the first thing so that any code reading it knows the right env. | ||
process.env.BABEL_ENV = 'test'; | ||
process.env.NODE_ENV = 'test'; | ||
const createJestConfig = require('./utils/createJestConfig'); | ||
const paths = require('./utils/paths'); | ||
const fs = require('fs'); | ||
const jest = require('jest'); | ||
|
||
const argv = process.argv.slice(2); | ||
|
||
let appPackageJson = {}; | ||
|
||
try { | ||
appPackageJson = fs.readJSONSync(paths.packageJson); | ||
} catch (e) { | ||
// do nothing | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
throw err; | ||
}); | ||
|
||
argv.push( | ||
'--config', | ||
JSON.stringify( | ||
Object.assign({}, createJestConfig(), appPackageJson.jest || {}) | ||
) | ||
); | ||
|
||
jest.run(argv); |
33 changes: 33 additions & 0 deletions
33
packages/contentful-extension-scripts/scripts/utils/createJestConfig.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,33 @@ | ||
const paths = require('./paths'); | ||
|
||
module.exports = function createJestConfig() { | ||
const config = { | ||
verbose: true, | ||
transform: { | ||
'.+\\.(js|jsx)?$': require.resolve('babel-jest'), | ||
'^.+\\.(ts|tsx)?$': require.resolve('ts-jest/dist'), | ||
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': require.resolve( | ||
'./jestFileTransform.js' | ||
), | ||
}, | ||
transformIgnorePatterns: [ | ||
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|tsx|ts)$', | ||
], | ||
moduleNameMapper: { | ||
'\\.(css|less|scss)$': require.resolve('identity-obj-proxy'), | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'], | ||
coverageReporters: ['lcov'], | ||
testMatch: ['<rootDir>/**/*.(spec|test).{ts,tsx,js,jsx}'], | ||
testURL: 'http://localhost', | ||
rootDir: paths.root, | ||
coverageDirectory: paths.coverage, | ||
watchPlugins: [ | ||
require.resolve('jest-watch-typeahead/filename'), | ||
require.resolve('jest-watch-typeahead/testname'), | ||
], | ||
}; | ||
|
||
return config; | ||
}; |
Empty file.
11 changes: 11 additions & 0 deletions
11
packages/contentful-extension-scripts/scripts/utils/jestFileTransform.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,11 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
// This is a custom Jest transformer turning file imports into filenames. | ||
module.exports = { | ||
process(src, filename) { | ||
const assetFilename = JSON.stringify(path.basename(filename)); | ||
return `module.exports = ${assetFilename};`; | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,7 @@ | |
[ | ||
"@babel/preset-env", | ||
{ | ||
"useBuiltIns": false, | ||
"modules": false | ||
"useBuiltIns": false | ||
} | ||
], | ||
[ | ||
|
5 changes: 3 additions & 2 deletions
5
packages/contentful-extension-scripts/template/common/.contentfulrc.json
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,5 +1,6 @@ | ||
{ | ||
"cmaToken": "", | ||
"managementToken": "", | ||
"activeSpaceId": "", | ||
"activeEnvironmentId": "" | ||
"activeEnvironmentId": "", | ||
"host": "api.contentful.com" | ||
} |
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 |
---|---|---|
|
@@ -15,6 +15,9 @@ yarn-error.log* | |
# Dependency directories | ||
node_modules/ | ||
|
||
# Coverage | ||
.coverage | ||
|
||
# Build | ||
build/* | ||
!/build/index.html |
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
50 changes: 50 additions & 0 deletions
50
packages/contentful-extension-scripts/template/javascript-entry/src/index.spec.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,50 @@ | ||
import React from 'react'; | ||
import { App } from './index'; | ||
import { render, cleanup, fireEvent, configure } from '@testing-library/react'; | ||
|
||
configure({ | ||
testIdAttribute: 'data-test-id', | ||
}); | ||
|
||
function renderComponent(sdk) { | ||
return render(<App sdk={sdk} />); | ||
} | ||
|
||
const sdk = { | ||
entry: { | ||
fields: { | ||
title: { getValue: jest.fn(), setValue: jest.fn() }, | ||
body: { getValue: jest.fn(), setValue: jest.fn() }, | ||
abstract: { getValue: jest.fn(), setValue: jest.fn() }, | ||
hasAbstract: { getValue: jest.fn(), setValue: jest.fn() }, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('App', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
afterEach(cleanup); | ||
|
||
it('should read a values from entry.fields.*', () => { | ||
sdk.entry.fields.title.getValue.mockReturnValue('title-value'); | ||
sdk.entry.fields.body.getValue.mockReturnValue('body-value'); | ||
sdk.entry.fields.hasAbstract.getValue.mockReturnValue(true); | ||
sdk.entry.fields.abstract.getValue.mockReturnValue('abstract-value'); | ||
const { getByTestId } = renderComponent(sdk); | ||
|
||
expect(getByTestId('field-title').value).toEqual('title-value'); | ||
expect(getByTestId('field-body').value).toEqual('body-value'); | ||
expect(getByTestId('field-abstract').value).toEqual('abstract-value'); | ||
|
||
fireEvent.change(getByTestId('field-body'), { | ||
target: { value: 'new-body-value' }, | ||
}); | ||
|
||
expect(sdk.entry.fields.body.setValue).toHaveBeenCalledWith( | ||
'new-body-value' | ||
); | ||
}); | ||
}); |
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
62 changes: 62 additions & 0 deletions
62
packages/contentful-extension-scripts/template/javascript-fields/src/index.spec.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,62 @@ | ||
import React from 'react'; | ||
import { App } from './index'; | ||
import { render, fireEvent, cleanup, configure } from '@testing-library/react'; | ||
|
||
configure({ | ||
testIdAttribute: 'data-test-id', | ||
}); | ||
|
||
function renderComponent(sdk) { | ||
return render(<App sdk={sdk} />); | ||
} | ||
|
||
const sdk = { | ||
field: { | ||
getValue: jest.fn(), | ||
onValueChanged: jest.fn(), | ||
setValue: jest.fn(), | ||
removeValue: jest.fn(), | ||
}, | ||
window: { | ||
startAutoResizer: jest.fn(), | ||
}, | ||
}; | ||
|
||
describe('App', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
afterEach(cleanup); | ||
|
||
it('should read a value from field.getValue() and subscribe for external changes', () => { | ||
sdk.field.getValue.mockImplementation(() => 'initial-value'); | ||
const { getByTestId } = renderComponent(sdk); | ||
|
||
expect(sdk.field.getValue).toHaveBeenCalled(); | ||
expect(sdk.field.onValueChanged).toHaveBeenCalled(); | ||
expect(getByTestId('my-field').value).toEqual('initial-value'); | ||
}); | ||
|
||
it('should call starstartAutoResizer', () => { | ||
renderComponent(sdk); | ||
expect(sdk.window.startAutoResizer).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call setValue on every change in input and removeValue when input gets empty', () => { | ||
const { getByTestId } = renderComponent(sdk); | ||
|
||
fireEvent.change(getByTestId('my-field'), { | ||
target: { value: 'new-value' }, | ||
}); | ||
|
||
expect(sdk.field.setValue).toHaveBeenCalledWith('new-value'); | ||
|
||
fireEvent.change(getByTestId('my-field'), { | ||
target: { value: '' }, | ||
}); | ||
|
||
expect(sdk.field.setValue).toHaveBeenCalledTimes(1); | ||
expect(sdk.field.removeValue).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.