Skip to content

Commit

Permalink
feat(taro-runner-utils): 添加单元测试和优化注释
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 authored and luckyadam committed Dec 31, 2019
1 parent d799097 commit 97a1642
Show file tree
Hide file tree
Showing 10 changed files with 4,353 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getBundleContent test sass resource array 1`] = `
"$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
"
`;

exports[`getBundleContent test sass resource array with directory 1`] = `
"$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
"
`;

exports[`getBundleContent test sass resource path 1`] = `
"$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
"
`;

exports[`getBundleContent test sass resource path with directory 1`] = `
"$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
"
`;

exports[`getBundleResult test file name and project directory path 1`] = `
"$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
"
`;

exports[`getBundleResult test file path 1`] = `
"$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
"
`;

exports[`getSassLoaderOption test get sass loader option 1`] = `
Object {
"data": "$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
",
"implementation": "dart-sass",
}
`;
91 changes: 91 additions & 0 deletions packages/taro-runner-utils/__tests__/scss.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import path from 'path'
import { getBundleResult, getBundleContent, getSassLoaderOption } from '../dist/index'

describe('getBundleResult', () => {
const filePath = path.resolve(__dirname, '.', 'styles/variables.scss')
const fileConfig = {
name: 'styles/variables.scss',
path: path.resolve(__dirname, '.')
}

test('test file path', async () => {
const res = await getBundleResult(filePath)
expect(res.bundledContent).toMatchSnapshot()
})

test('test file name and project directory path', async () => {
const res = await getBundleResult(fileConfig.name, fileConfig.path)
expect(res.bundledContent).toMatchSnapshot()
})
})

describe('getBundleContent', () => {
const sassResourcePath = {
resource: path.resolve(__dirname, '.', 'styles/variables.scss')
}
const sassResourcePathWithDirectory = {
resource: 'styles/variables.scss',
projectDirectory: path.resolve(__dirname, '.')
}
const sassResourceArray = {
resource: [
path.resolve(__dirname, '.', 'styles/variables.scss'),
path.resolve(__dirname, '.', 'styles/mixins.scss')
]
}
const sassResourceArrayWithDirectory = {
resource: [
'styles/variables.scss',
'styles/mixins.scss'
],
projectDirectory: path.resolve(__dirname, '.')
}

test('test sass resource path', async () => {
const data = sassResourcePath;
const res = await getBundleContent(data.resource)
expect(res).toMatchSnapshot()
})
test('test sass resource path with directory', async () => {
const data = sassResourcePathWithDirectory
const res = await getBundleContent(data.resource, data.projectDirectory)
expect(res).toMatchSnapshot()
})

test('test sass resource array', async () => {
const data = sassResourceArray;
const res = await getBundleContent(data.resource)
expect(res).toMatchSnapshot()
})
test('test sass resource array with directory', async () => {
const data = sassResourceArrayWithDirectory
const res = await getBundleContent(data.resource, data.projectDirectory)
expect(res).toMatchSnapshot()
})
})

describe('getSassLoaderOption', () => {
const buildConfig = {
publicPath: '/',
staticDirectory: 'static',
deviceRatio: { '640': 1.17, '750': 1, '828': 0.905 },
designWidth: 750,
outputRoot: 'dist',
sass: {
resource: [
'styles/variables.scss',
'styles/mixins.scss'
],
projectDirectory: path.resolve(__dirname, '.')
},
sassLoaderOption: {
implementation: 'dart-sass'
},
sourceRoot: 'src'
}

test('test get sass loader option', async () => {
const res = await getSassLoaderOption(buildConfig)
expect(res).toMatchSnapshot()
})
})
5 changes: 5 additions & 0 deletions packages/taro-runner-utils/__tests__/styles/mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
4 changes: 4 additions & 0 deletions packages/taro-runner-utils/__tests__/styles/variables.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$color-ui-info: #78A4FA !default;
$color-ui-positive: #13CE66 !default;
$color-ui-negative: #FF4949 !default;
$color-ui-warning: #FFC82C !default;
12 changes: 12 additions & 0 deletions packages/taro-runner-utils/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
}
10 changes: 10 additions & 0 deletions packages/taro-runner-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
verbose: true,
moduleFileExtensions: ['js', 'jsx', 'json'],
rootDir: __dirname,
testMatch: ['<rootDir>/__tests__/**/*.test.js'],
transform: {
'^.+\\.js?$': 'babel-jest'
},
transformIgnorePatterns: ['<rootDir>/node_modules/']
}
5 changes: 5 additions & 0 deletions packages/taro-runner-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"author": "garfield550",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@types/jest": "^24.0.23",
"babel-jest": "^24.9.0",
"jest": "^24.9.0",
"typescript": "^3.7.3"
},
"dependencies": {
Expand Down
15 changes: 14 additions & 1 deletion packages/taro-runner-utils/src/scss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ interface BuildConfig {
* Return bundled sass content.
*
* @param {string} url Absolute file path.
* @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located. Used for resolving tilde imports.
* @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located.
* Used for resolving tilde imports.
* @returns Bundle result.
*/
export async function getBundleResult(url: string,
Expand All @@ -32,6 +33,13 @@ export async function getBundleResult(url: string,
return res
}

/**
* Return bundled sass content, but input resource can be a single string or an array.
* @param {(string | string[])} resource Input file path or a path array.
* @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located.
* Used for resolving tilde imports.
* @returns Bundle result.
*/
export async function getBundleContent(resource: string | string[],
projectDirectory: string | undefined = undefined
): Promise<string | undefined> {
Expand All @@ -56,6 +64,11 @@ export async function getBundleContent(resource: string | string[],
return result
}

/**
* Return the merged sass loader option.
* @param {BuildConfig} param0 Build config.
* @returns Merged sass loader option.
*/
export async function getSassLoaderOption(
{ sass, sassLoaderOption }: BuildConfig
): Promise<LoaderOption> {
Expand Down
15 changes: 14 additions & 1 deletion packages/taro-runner-utils/types/scss.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,23 @@ interface BuildConfig {
* Return bundled sass content.
*
* @param {string} url Absolute file path.
* @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located. Used for resolving tilde imports.
* @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located.
* Used for resolving tilde imports.
* @returns Bundle result.
*/
export declare function getBundleResult(url: string, projectDirectory?: string | undefined): Promise<BundleResult>;
/**
* Return bundled sass content, but input resource can be a single string or an array.
* @param {(string | string[])} resource Input file path or a path array.
* @param {(string | undefined)} projectDirectory Absolute project location, where node_modules are located.
* Used for resolving tilde imports.
* @returns Bundle result.
*/
export declare function getBundleContent(resource: string | string[], projectDirectory?: string | undefined): Promise<string | undefined>;
/**
* Return the merged sass loader option.
* @param {BuildConfig} param0 Build config.
* @returns Merged sass loader option.
*/
export declare function getSassLoaderOption({ sass, sassLoaderOption }: BuildConfig): Promise<LoaderOption>;
export default getSassLoaderOption;
Loading

0 comments on commit 97a1642

Please sign in to comment.