Skip to content

Commit

Permalink
implement view snapshot button (#197)
Browse files Browse the repository at this point in the history
This commit implements issue #186. I can't know if there is a snapshot
for each but I can add a button to the summary that opens the snapshot
for the file in the code editor. That's what this commit does. It adds
the button to the UI and a mutation to send to the server to open the
snapshot in the editor.
And it implements issue #191. There was already a button for generating
a snapshot, but it was only visible when there was a detected snapshot
error. This caused us to have to run known failing tests before we could
get the button to generate the snapshots. Now the button is there all
the time, so if you know your snapshots are out of date, then you can
generate them immediately and save time.

Co-authored-by: Greg Veres <greg.veres@rogers.com>
  • Loading branch information
gregveres and Greg Veres authored May 21, 2020
1 parent bb318f7 commit f31897c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
14 changes: 14 additions & 0 deletions server/api/app/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as launch from "launch-editor";
import { App } from "./app";
import FileWatcher, { WatcherEvents } from "../../services/file-watcher";
import { pubsub } from "../../event-emitter";
import { dirname, basename } from "path";

@Resolver(App)
export default class AppResolver {
Expand Down Expand Up @@ -45,7 +46,20 @@ export default class AppResolver {
return "";
}

@Mutation(returns => String)
openSnapInEditor(@Arg("path") path: string) {
var dir = dirname(path)
var file = basename(path);

var snap = dir + '/__snapshots__/' + file + '.snap'
console.log("opening the snapshot:", snap);
this.openInEditor(snap);

return "";
}

@Mutation(returns => String)

openFailure(@Arg("failure") failure: string) {
// The following regex matches the first line of the form: \w at <some text> (<file path>)
// it captures <file path> and returns that in the second position of the match array
Expand Down
9 changes: 0 additions & 9 deletions ui/test-file/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ function TestFile({ selectedFilePath, isRunning, projectRoot, onStop }: Props) {

const isUpdating = isRunning && (result === null ||(result.numPassingTests === 0 && result.numFailingTests === 0));

const haveSnapshotFailures = ((result && result.testResults) || []).some(
testResult => {
return (testResult.failureMessages || []).some(failureMessage =>
failureMessage.includes("toMatchSnapshot")
);
}
);

const roots = (fileItemResult.items || []).filter(
item => item.parent === null
);
Expand All @@ -123,7 +115,6 @@ function TestFile({ selectedFilePath, isRunning, projectRoot, onStop }: Props) {
isRunning={isRunning}
isUpdating={isUpdating}
isLoadingResult={loading}
haveSnapshotFailures={haveSnapshotFailures}
onRun={() => {
runFile();
}}
Expand Down
24 changes: 19 additions & 5 deletions ui/test-file/summary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
CheckCircle,
Frown,
ZapOff,
Circle
Circle,
Eye
} from "react-feather";
import Button from "../../components/button";
import OPEN_IN_EDITOR from "./open-in-editor.gql";
import OPEN_SNAP_IN_EDITOR from "./open-snap-in-editor.gql";
import { Tooltip } from "react-tippy";
import { useMutation } from "react-apollo-hooks";

Expand Down Expand Up @@ -130,7 +132,6 @@ export default function FileSummary({
onRun,
onStop,
onSnapshotUpdate,
haveSnapshotFailures
}: Props) {
const Icon = isRunning ? StopCircle : Play;

Expand All @@ -140,6 +141,12 @@ export default function FileSummary({
}
});

const openSnapshotInEditor = useMutation(OPEN_SNAP_IN_EDITOR, {
variables: {
path
}
});

return (
<Container p={4} bg="slightDark">
{( isUpdating || isLoadingResult) && <ContainerBG />}
Expand Down Expand Up @@ -191,9 +198,8 @@ export default function FileSummary({
}}
/>
</Tooltip>
{haveSnapshotFailures && (
<Tooltip
title="Update all snapshots of the file"
title="Update all snapshots for this file"
position="bottom"
size="small"
>
Expand All @@ -207,7 +213,15 @@ export default function FileSummary({
Update Snapshot
</Button>
</Tooltip>
)}
<Tooltip title="Open snapshot in editor" size="small" position="bottom">
<Button
icon={<Eye size={14} />}
minimal
onClick={() => {
openSnapshotInEditor();
}}
/>
</Tooltip>
</ActionPanel>
</Container>
);
Expand Down
3 changes: 3 additions & 0 deletions ui/test-file/summary/open-snap-in-editor.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutation OpenSnapInEditor($path: String!) {
openSnapInEditor(path: $path)
}

0 comments on commit f31897c

Please sign in to comment.