Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 7, 2023
1 parent b0d4276 commit ea84379
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 34 deletions.
15 changes: 10 additions & 5 deletions packages/canyon-data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,22 @@ export const getSummaryByPath = (
export const genSummaryTreeItem = (
path: string,
summary: CoverageSummaryMap,
) => {

console.log(path,'sss')

):{
path: string;
summary: CoverageSummaryData;
children: {
path: string;
summary: CoverageSummaryData;
}[];
} => {
function check(item: string, path: string) {
if (path === '') {
if (path === '~') {
return true;
}
return item.includes(path);
}

// 如果是文件
if (Object.keys(summary).find((item) => item === path)) {
return {
path,
Expand Down
45 changes: 31 additions & 14 deletions packages/canyon-report/src/Report/components/IstanbulReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { CoverageSummaryMap, genSummaryTreeItem } from '@canyon/data';
import { FC, useMemo } from 'preact/compat';
import { useEffect, useState } from 'preact/hooks';

import { IstanbulReportProps } from '../types';
import { Dims } from '../types.ts';
import Th from './Th.tsx';
import Tr from './Tr.tsx';
import { Dims, IstanbulReportProps } from '../types';
// import { Dims } from '../types';
import Th from './Th';
import Tr from './Tr';

const capitalized = (word: string) =>
word.charAt(0).toUpperCase() + word.slice(1);
Expand All @@ -29,10 +29,13 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
watermarks,
}) => {
const [summary, setSummary] = useState<CoverageSummaryMap>({});
// @ts-ignore
const [fileCoverage, setFileCoverage] = useState({} as any);
const [fileContent, setFileContent] = useState('');
const [activePath, setActivePath] = useState('~');

const summaryTreeItem = useMemo(() => {
return genSummaryTreeItem('~', summary);
return genSummaryTreeItem(activePath, summary);
}, [activePath, summary]);
const paths = useMemo(() => {
return activePath
Expand All @@ -51,6 +54,15 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
);
}, [activePath]);

useEffect(() => {
if (activePath.includes('.')) {
onSelectFile(activePath).then(({ fileContent, fileCoverage }) => {
setFileContent(fileContent);
setFileCoverage(fileCoverage);
});
}
}, [activePath]);

useEffect(() => {
const handleSetOptionEvent = (e: { detail: any }) => {
setSummary(e.detail);
Expand All @@ -64,19 +76,19 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
};
}, []); // 空数组表示仅在组件挂载和卸载时运行


return (
<div id={'canyon-report'}>
<p>{activePath}</p>
<div className={'pad1'}>
<h1>
<a
onClick={() => {
setActivePath('');
setActivePath('~');
}}
>
All files
</a>{' '}
/{' '}
</a>
<span> / </span>
<div className="pathnames" style="display: inline">
{paths.map(({ path, name }) => {
return (
Expand All @@ -92,7 +104,8 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
</>
);
})}
</div>{' '}
</div>
<span>&nbsp;</span>
<span className="filename">
{summaryTreeItem.path.split('/').at(-1)}
</span>
Expand All @@ -101,11 +114,14 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
{dims.map((dim) => (
<div className="fl pad1y space-right2">
<span className="strong">
{/*// @ts-ignore*/}
{summaryTreeItem.summary[dim].pct}%
</span>
<span className="quiet">{capitalized(dim)}</span>
<span className="fraction">
{/*// @ts-ignore*/}
{summaryTreeItem.summary.statements.covered}/
{/*// @ts-ignore*/}
{summaryTreeItem.summary[dim].total}
</span>
</div>
Expand Down Expand Up @@ -133,14 +149,15 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
<table className="coverage-summary">
<Th />
<tbody>
{summaryTreeItem.children.map((item) => {
{/*// @ts-ignore*/}
{summaryTreeItem.children.map(({ path, summary }) => {
return (
<Tr
setActivePath={(p) => {
setActivePath(p);
}}
path={item.path}
item={item.summary}
path={path}
item={summary}
watermarks={watermarks}
/>
);
Expand All @@ -149,7 +166,7 @@ const IstanbulReport: FC<IstanbulReportProps> = ({
</table>
</div>
) : (
<div>wu</div>
<div>{fileContent}</div>
)}
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/canyon-report/src/Report/components/Tr.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FC } from 'preact/compat';

import { Dims, TrProps, Watermarks } from '../types.ts';
import { Dims, TrProps, Watermarks } from '../types';

const classForPercent = (
type: Dims,
value: number,
_watermarks: Watermarks,
) => {
const watermarks = _watermarks[type];
const watermarks = _watermarks[type] as any;
if (!watermarks) {
return 'unknown';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/canyon-report/src/Report/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Init from './init.tsx';
import Init from './init';

export * from './types';

Expand Down
11 changes: 7 additions & 4 deletions packages/canyon-report/src/Report/init.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { render } from 'preact';

import IstanbulReport from './components/IstanbulReport';
import { Watermarks } from './types';
import { OnSelectFile, Watermarks } from './types';

function noop(path: string) {
function noop(
path: string,
): Promise<{ fileContent: string; fileCoverage: any }> {
console.log(path, 'path');
return new Promise((resolve) => {
resolve({ name: path });
resolve({ fileCoverage: {}, fileContent: `` });
});
}
const configWatermarks = {
Expand All @@ -19,7 +22,7 @@ const init = (
{
onSelectFile = noop,
watermarks = configWatermarks,
}: { onSelectFile: (path: string) => Promise<any>; watermarks: Watermarks },
}: { onSelectFile: OnSelectFile; watermarks: Watermarks },
) => {
render(
<IstanbulReport onSelectFile={onSelectFile} watermarks={watermarks} />,
Expand Down
11 changes: 4 additions & 7 deletions packages/canyon-report/src/Report/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
export type ReportType = {
init: (options: any) => void;
setOption: (options: any) => void;
}
export type Init = {

}
export enum Dims {
statements = 'statements',
functions = 'functions',
branches = 'branches',
lines = 'lines',
}

export type OnSelectFile = (
path: string,
) => Promise<{ fileContent: string; fileCoverage: any }>;

export type Watermarks = {
[Dims.statements]: number[];
[Dims.functions]: number[];
Expand Down
6 changes: 5 additions & 1 deletion packages/canyon-report/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ const configWatermarks = {
};
const report = init(document.querySelector('#root') as any, {
onSelectFile(path: string) {
console.log(path, 'path');
return new Promise((resolve) => {
resolve(path);
resolve({
fileCoverage: {},
fileContent: `const a = 1`,
});
});
},
watermarks: configWatermarks,
Expand Down

0 comments on commit ea84379

Please sign in to comment.