Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Oct 29, 2024
1 parent f6f429e commit 5feb85e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 49 deletions.
5 changes: 1 addition & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ exports.createWebpackConfig = async function (component) {
}

const karmaConfTpl = `${readConfigTpl}
module.exports = require('../share/karma.conf')(config.name, {
hasStyle: config.style,
useIcon: config.icon,
})
module.exports = require('../share/karma.conf')(config.name)
`

exports.createKarmaConf = async function (component) {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@babel/core": "^7.10.5",
"@babel/preset-react": "^7.22.5",
"@jsdevtools/coverage-istanbul-loader": "^3.0.5",
"@metahub/karma-postcss-preprocessor": "^4.0.1",
"@storybook/addon-knobs": "^6.2.9",
"@storybook/addon-storysource": "^6.4.14",
"@storybook/addons": "^6.4.14",
Expand All @@ -63,7 +62,6 @@
"karma-coverage-istanbul-reporter": "^3.0.3",
"karma-jquery": "^0.2.4",
"karma-mocha": "^2.0.1",
"karma-scss-preprocessor": "^4.0.0",
"karma-sourcemap-loader": "^0.3.8",
"karma-webpack": "^4.0.2",
"mini-css-extract-plugin": "^0.12.0",
Expand Down
32 changes: 25 additions & 7 deletions src/logcat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class Logcat extends Component<IOptions> {

this.initOptions(options, {
entries: [],
wrapLongLines: true,
wrapLongLines: false,
})

this.render = throttle(() => this._render(), 16)
Expand All @@ -88,12 +88,13 @@ export default class Logcat extends Component<IOptions> {
const level = [' ', 'V', 'D', 'I', 'W', 'E'][entry.priority - 1]

const container = h(`.${c('entry')}.${c(level)}`)
const e = {
...entry,
date,
}
this.entries.push({
container,
entry: {
...entry,
date,
},
entry: e,
})

const html = [
Expand All @@ -111,12 +112,27 @@ export default class Logcat extends Component<IOptions> {
].join(' ')
container.innerHTML = html

this.container.appendChild(container)
if (this.filterEntry(e)) {
this.container.appendChild(container)
}
}
clear() {
this.entries = []
this.$container.html('')
}
private filterEntry(entry: IBaseEntry) {
const { filter } = this.options

if (!filter) {
return true
}

if (filter.priority && entry.priority < filter.priority) {
return false
}

return true
}
private bindEvent() {
const { c } = this

Expand All @@ -140,7 +156,9 @@ export default class Logcat extends Component<IOptions> {
this.$container.html('')

each(this.entries, (entry) => {
container.appendChild(entry.container)
if (this.filterEntry(entry.entry)) {
container.appendChild(entry.container)
}
})
}
}
Expand Down
24 changes: 21 additions & 3 deletions src/logcat/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import readme from './README.md'
import story from '../share/story'
import each from 'licia/each'
import $ from 'licia/$'
import { boolean, number } from '@storybook/addon-knobs'
import { boolean, number, select } from '@storybook/addon-knobs'
import logs from './logcat.json'

const def = story(
'logcat',
(container) => {
$(container).css('height', '500px')

const { wrapLongLines } = createKnobs()
const { wrapLongLines, filter } = createKnobs()

const logcat = new Logcat(container, {
filter,
wrapLongLines,
})
each(logs, (log) => logcat.append(log))
Expand All @@ -28,9 +29,26 @@ const def = story(
)

function createKnobs() {
const wrapLongLines = boolean('Wrap Long Lines', true)
const priority = select(
'Filter Level',
{
DEFAULT: 1,
VERBOSE: 2,
DEBUG: 3,
INFO: 4,
WARN: 5,
ERROR: 6,
FATAL: 7,
SILENT: 8,
},
1
)
const wrapLongLines = boolean('Wrap Long Lines', false)

return {
filter: {
priority,
},
wrapLongLines,
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/logcat/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Logcat from './index'
import test from '../share/test'

test('log', (container) => {
const logcat = new Logcat(container)

it('basic', () => {
logcat.append({
date: '2024-10-28T07:21:37.452Z',
pid: 31332,
tid: 17073,
priority: 5,
tag: 'System.err',
message:
'java.lang.NoSuchMethodException: android.view.IWindowManager$Stub$Proxy.getRotation []',
package: 'com.example',
})
})

return logcat
})
35 changes: 2 additions & 33 deletions src/share/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
const path = require('path')
const each = require('licia/each')
const contain = require('licia/contain')
const endWith = require('licia/endWith')
const { getFullDependencies, readComponentConfig } = require('../../lib/util')

const headless = contain(process.argv, '--headless')

module.exports = function (name, { useIcon = false, hasStyle = true } = {}) {
module.exports = function (name) {
const webpackCfg = require(`../${name}/webpack.config.js`)(
{},
{ mode: 'development' }
)
const scssRule = webpackCfg.module.rules[1]
scssRule.loaders.shift()
scssRule.loaders.unshift('style-loader')
const cssRule = webpackCfg.module.rules[2]
cssRule.loaders.shift()
cssRule.loaders.unshift('style-loader')
webpackCfg.module.rules.push({
test: /\.ts$/,
exclude: /node_modules|share/,
Expand All @@ -41,28 +34,9 @@ module.exports = function (name, { useIcon = false, hasStyle = true } = {}) {
}
files.push(`../../dist/${dependency}/luna-${dependency}.js`)
})
files.push(`../../dist/${name}/luna-${name}.css`)
files.push('test.js')

const postcssLoader = cssRule.loaders[2]
const postcssOptions = postcssLoader.options

const styles = []
if (hasStyle) {
styles.push('style.scss')
}
if (useIcon) {
styles.push('icon.css')
}
each(styles, (style) => {
const file = `../${name}/${style}`
files.unshift(file)
const preprocessor = ['postcss']
if (endWith(style, 'scss')) {
preprocessor.unshift('scss')
}
preprocessors[file] = preprocessor
})

return function (config) {
config.set({
basePath: `../${name}`,
Expand All @@ -79,18 +53,13 @@ module.exports = function (name, { useIcon = false, hasStyle = true } = {}) {
'karma-sourcemap-loader',
'karma-chrome-launcher',
'karma-coverage-istanbul-reporter',
'karma-scss-preprocessor',
'@metahub/karma-postcss-preprocessor',
],
coverageIstanbulReporter: {
reports: ['html', 'lcovonly', 'text', 'text-summary'],
dir: path.join(__dirname, `../../coverage/${name}`),
},
reporters: ['progress', 'coverage-istanbul'],
webpack: webpackCfg,
postcssPreprocessor: {
options: postcssOptions,
},
preprocessors,
browsers: [headless ? 'ChromeHeadless' : 'Chrome'],
browserNoActivityTimeout: 100000,
Expand Down

0 comments on commit 5feb85e

Please sign in to comment.