mirror of
https://github.com/cypress-io/cypress.git
synced 2026-04-22 23:20:24 -05:00
cc3be10f73
* move to ts * refactor * test * type * fix tests * types * simplify update preferences via graphql * types * simplify * show config in editor * fix test * add description for mutation and update default prefernces * update schema * update docs * update description of localSettings field Co-authored-by: Mark Noonan <mark@cypress.io> Co-authored-by: Mark Noonan <oddlyaromatic@gmail.com>
70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import * as savedState from './saved_state'
|
|
import { Command, SaveDetails, createNewTestInFile, appendCommandsToTest, createNewTestInSuite, convertCommandsToText } from './util/spec_writer'
|
|
|
|
interface SaveInfo extends SaveDetails {
|
|
isSuite: boolean
|
|
isRoot: boolean
|
|
}
|
|
|
|
class StudioSaveError extends Error {
|
|
static errMessage = (isSuite) => `Studio was unable to find your ${isSuite ? 'suite' : 'test'} in the spec file.`
|
|
|
|
constructor (isSuite) {
|
|
super(StudioSaveError.errMessage(isSuite))
|
|
this.name = 'StudioSaveError'
|
|
}
|
|
}
|
|
|
|
export const setStudioModalShown = () => {
|
|
return savedState.create()
|
|
.then((state) => {
|
|
state.set({ showedStudioModal: true })
|
|
})
|
|
}
|
|
|
|
export const getStudioModalShown = () => {
|
|
return savedState.create()
|
|
.then((state) => state.get())
|
|
.then((state) => !!state.showedStudioModal)
|
|
}
|
|
|
|
export const save = (saveInfo: SaveInfo) => {
|
|
const { isSuite, isRoot, absoluteFile, commands, testName } = saveInfo
|
|
|
|
const saveToFile = () => {
|
|
if (isRoot) {
|
|
return createNewTestInFile(absoluteFile, commands, testName || 'New Test')
|
|
}
|
|
|
|
if (isSuite) {
|
|
return createNewTestInSuite(saveInfo)
|
|
}
|
|
|
|
return appendCommandsToTest(saveInfo)
|
|
}
|
|
|
|
return saveToFile()
|
|
.then((success) => {
|
|
return setStudioModalShown()
|
|
.then(() => {
|
|
if (!success) {
|
|
throw new StudioSaveError(isSuite)
|
|
}
|
|
|
|
return null
|
|
})
|
|
})
|
|
.catch((err) => {
|
|
return {
|
|
type: err.type,
|
|
name: err.name,
|
|
message: err.message,
|
|
stack: err.stack,
|
|
}
|
|
})
|
|
}
|
|
|
|
export const getCommandsText = (commands: Command[]) => {
|
|
return convertCommandsToText(commands)
|
|
}
|