Newer
Older
import React from 'react'
import { useDropzone } from 'react-dropzone'
import {
FileWithName,
Formalization,
Implementation,
Pattern,
} from 'src/models'
import {
MAX_FILE_SIZE,
SIZE_TOO_LARGE,
} from 'src/pages/patterns/add-pattern/use-add-formalizations'
import { GetFormalizationsQuery } from 'src/pages/patterns/query-builder/get-formalizations-query'
import { RunTestsStep1Mutation } from '../query-builder/run-tests-step-1-mutation'
import { RunTestsStep2Mutation } from '../query-builder/run-tests-step-2-mutation'
export function useRunTestsButton({
patternId,
implementationId,
}: {
patternId: Pattern['id']
implementationId: Implementation['id']
React.useEffect(() => {
queryClient.invalidateQueries(GetFormalizationsQuery.key)
}, [])
// Formalization
const [formalizationId, setFormalizationId] =
React.useState<Formalization['id']>('')
const formalizationsQuery = GetFormalizationsQuery.useQuery(
{
patternId: patternId,
},
{
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
setFormalizationId(data[0]?.id ?? '')
},
}
)
// Strategy
const [strategy, setStrategy] = React.useState<FileWithName>()
const onDropStrategy = React.useCallback((acceptedFiles: File[]) => {
if (acceptedFiles.length > 1) {
throw Error('Expected 1 strategy file')
}
const res = acceptedFiles.map(
(f) =>
({
file: f,
name: f.name.split('.')[0] ?? f.name,
} as FileWithName)
)
setStrategy(res[0])
return [...res]
}, [])
const strategyDropzoneState = useDropzone({
onDrop: onDropStrategy,
noDrag: true,
validator: (file) => {
if (file.size > MAX_FILE_SIZE) {
return {
code: SIZE_TOO_LARGE,
message: `Maximo ${MAX_FILE_SIZE} MB.`,
}
}
return null
},
})
// Test cases instantiation
const [testCasesInstantiation, setTestCasesInstantiation] =
React.useState<FileWithName>()
const onDropTestCasesInstantiation = React.useCallback(
(acceptedFiles: File[]) => {
if (acceptedFiles.length > 1) {
throw Error('Expected 1 test file')
}
const res = acceptedFiles.map(
(f) =>
({
file: f,
name: f.name.split('.')[0] ?? f.name,
} as FileWithName)
)
return [...res]
},
[]
)
const testCasesInstantiationDropzoneState = useDropzone({
noDrag: true,
validator: (file) => {
if (file.size > MAX_FILE_SIZE) {
return {
code: SIZE_TOO_LARGE,
message: `Maximo ${MAX_FILE_SIZE} MB.`,
}
}
return null
},
})
const onRemoveTestCasesInstantiation = () => setTestCasesInstantiation(null)
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Wrapper
const [wrapper, setWrapper] = React.useState<FileWithName>()
const onDropWrapper = React.useCallback((acceptedFiles: File[]) => {
if (acceptedFiles.length > 1) {
throw Error('Expected 1 test file')
}
const res = acceptedFiles.map(
(f) =>
({
file: f,
name: f.name.split('.')[0] ?? f.name,
} as FileWithName)
)
setWrapper(res[0])
return [...res]
}, [])
const wrapperDropzoneState = useDropzone({
onDrop: onDropWrapper,
noDrag: true,
validator: (file) => {
if (file.size > MAX_FILE_SIZE) {
return {
code: SIZE_TOO_LARGE,
message: `Maximo ${MAX_FILE_SIZE} MB.`,
}
}
return null
},
})
const onRemoveWrapper = () => setWrapper(null)
// Utils
const isValid = React.useMemo(() => {
return (
Boolean(formalizationId) &&
Boolean(strategy) &&
}, [formalizationId, strategy, testCasesInstantiation, name])
const [step, setStep] = React.useState<Step>('Generating')
const [genericJUnitPath, setGenericJUnitPath] = React.useState<string>(null)
id: formalizationId,
strategyFile: strategy.file,
testCasesInstantiationFile: testCasesInstantiation.file,
})
onSuccess: (report) => {
navigate(routes.report({ id: report.id }))
},
})
const handleOnRunTestsStep2 = async () => {
setStep('Loading')
id: formalizationId,
implementationId,
wrapperFile: wrapper.file,
toast({
status: 'success',
position: 'top',
title: `Se han corrido los tests satisfactoriamente.`,
})
formalizations: formalizationsQuery.data ?? [],
formalizationId,
setFormalizationId,
isValid,
strategyDropzoneState,
handleOnRunTestsStep1,
handleOnRunTestsStep2,
wrapper,
onRemoveWrapper,
wrapperDropzoneState,