Skip to content

Commit

Permalink
feat(design-system): FileExplorer (#15513)
Browse files Browse the repository at this point in the history
* wip: cleaning up the specs list and swapping it to be a filetree

* wip: refactor

* wip: refactor

* wip: work on refactor

* chore: improve types

* styling

* chore: remove all references to Cypress in tree list

* chore: remove all references to Cypress in tree list

* extract spec list component

* write some tests

* correctly update state

* chore: refactor

* update test

* make props optional

* optimizations

* add back search

* use memo

* run spec

* run spec

* fix a11l nav

* add tests for spec list a11y nav

* fix tests:

* remove unused

* update version

Co-authored-by: Jessica Sachs <jess@jessicasachs.io>
  • Loading branch information
lmiller1990 and JessicaSachs committed Mar 17, 2021
1 parent 3e9d752 commit f2b880c
Show file tree
Hide file tree
Showing 28 changed files with 1,032 additions and 653 deletions.
8 changes: 8 additions & 0 deletions npm/design-system/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"watch-ignore": [
"./test/_test-output",
"node_modules"
],
"require": "ts-node/register",
"exit": true
}
3 changes: 1 addition & 2 deletions npm/design-system/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
"componentFolder": "src",
"experimentalComponentTesting": true,
"experimentalFetchPolyfill": true,
"fixturesFolder": false,
"supportFile": false
"fixturesFolder": false
}
2 changes: 2 additions & 0 deletions npm/design-system/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'regenerator-runtime/runtime'
import 'cypress-real-events/support'
4 changes: 4 additions & 0 deletions npm/design-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"@fortawesome/free-brands-svg-icons": "5.15.2",
"@fortawesome/free-solid-svg-icons": "5.15.2",
"@fortawesome/react-fontawesome": "0.1.14",
"@iconify/icons-vscode-icons": "1.1.1",
"@iconify/react": "2.0.0-rc.8",
"classnames": "2.2.6",
"debug": "4.3.2"
},
Expand All @@ -41,6 +43,8 @@
"babel-loader": "8.0.6",
"css-loader": "2.1.1",
"cypress": "0.0.0-development",
"cypress-real-events": "1.1.0",
"mocha": "^7.0.1",
"react": "16.8.6",
"react-dom": "16.8.6",
"rollup": "^2.38.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@use '../../index.scss' as *;

.nav {
user-select: none;
white-space: nowrap;
}

.ul {
margin-block-start: 0;
margin-block-end: 0;
margin-inline-start: 0;
margin-inline-end: 0;
padding-inline-start: 0;
&:before {
display: none;
}
}

.li.li {
padding-left: 20px;
}


.ul, .li {
position: relative;
list-style: none;
font-size: $text-s;
line-height: 1.6;
}

.a {
position: relative;
color: unset;
text-decoration: none;
display: inline-block;
width: 100%;
&:hover {
cursor: pointer;
}
}

.ul .ul {
margin-inline-start: $text-xs;
}

.isSelected, .isSelected:hover {
text-decoration: underline;
}
130 changes: 130 additions & 0 deletions npm/design-system/src/components/FileExplorer/FileExplorer.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { mount } from '@cypress/react'
import React from 'react'
import { FileExplorer, FileComponentProps, FolderComponentProps } from './FileExplorer'
import { FileNode, makeFileHierarchy, TreeNode } from './helpers/makeFileHierarchy'

import styles from './FileExplorer.module.scss'

const specs: Cypress.Cypress['spec'][] = [
{
relative: 'foo/bar/foo.spec.js',
absolute: 'Users/code/foo/bar/foo.spec.js',
name: 'foo/bar/foo.spec.js',
},
{
relative: 'bar/foo.spec.tsx',
absolute: 'bar/foo.spec.tsx',
name: 'bar/foo.spec.tsx',
},
{
relative: 'merp/map.spec.ts',
absolute: 'merp/map.spec.ts',
name: 'merp/map.spec.ts',
},
]

interface FileExplorerTestProps {
clickFileStub: typeof cy.stub
clickFolderStub: typeof cy.stub
}

function createFileExplorer (testProps: FileExplorerTestProps): React.FC {
return () => {
const [selectedFile, setSelectedFile] = React.useState<string>()

const onFileClick = (file: FileNode) => {
setSelectedFile(file.absolute)
}

const files = makeFileHierarchy(specs.map((spec) => spec.relative))

const FileComponent: React.FC<FileComponentProps> = (props) => {
return (
<div onClick={() => {
testProps.clickFileStub(props.item)
props.onClick(props.item)
}}>
{props.item.name}
</div>
)
}

const FolderComponent: React.FC<FolderComponentProps> = (props) => {
return (
<div onClick={() => {
testProps.clickFolderStub()
props.onClick()
}}>
{props.item.name}
</div>
)
}

return (
<FileExplorer
files={files}
cssModule={styles}
selectedFile={selectedFile}
fileComponent={FileComponent}
folderComponent={FolderComponent}
onFileClick={onFileClick}
/>
)
}
}

describe('FileExplorer', () => {
it('basic usage', () => {
const files: TreeNode[] = [
{
type: 'folder',
name: 'foo',
absolute: 'foo',
files: [
{
type: 'file',
name: 'bar.js',
absolute: 'foo/bar.js',
},
],
},
]

const FileComponent: React.FC<FileComponentProps> = (props) => <div>{props.item.name}</div>
const FolderComponent: React.FC<FolderComponentProps> = (props) => <div>{props.item.name}</div>

mount(
<FileExplorer
files={files}
selectedFile={undefined}
fileComponent={FileComponent}
folderComponent={FolderComponent}
onFileClick={() => {}}
/>,
)
})

it('clicks file and folders', () => {
const clickFolderStub = cy.stub()
const clickFileStub = cy.stub()

const Wrapper = createFileExplorer({
clickFolderStub,
clickFileStub,
})

mount(<Wrapper />)

cy.get('div').contains('bar').click().then(() => {
expect(clickFolderStub).to.have.been.calledWith()
})

cy.get('div').contains('map.spec.ts').click().then(() => {
expect(clickFileStub).to.have.been.calledWith({
type: 'file',
absolute: 'merp/map.spec.ts',
name: 'map.spec.ts',
})
})
})
})
Loading

4 comments on commit f2b880c

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f2b880c Mar 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.8.0/circle-develop-f2b880c09de5c4490027689af86b6844706c8a6b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f2b880c Mar 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.8.0/appveyor-develop-f2b880c09de5c4490027689af86b6844706c8a6b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f2b880c Mar 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.8.0/appveyor-develop-f2b880c09de5c4490027689af86b6844706c8a6b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f2b880c Mar 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.8.0/circle-develop-f2b880c09de5c4490027689af86b6844706c8a6b/cypress.tgz

Please sign in to comment.