Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updating the test role page without reload #1754

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ import PersistStore from "../../../../main/PersistStore";
import values from "@/util/values";

import { HorizontalDotsMinor, FileMinor } from "@shopify/polaris-icons"
import LocalStore from "../../../../main/LocalStorageStore";

function ApiDetails(props) {

const { showDetails, setShowDetails, apiDetail, headers, getStatus, isGptActive } = props

const localCategoryMap = LocalStore.getState().categoryMap
const localSubCategoryMap = LocalStore.getState().subCategoryMap

const [sampleData, setSampleData] = useState([])
const [paramList, setParamList] = useState([])
const [selectedUrl, setSelectedUrl] = useState({})
Expand All @@ -36,8 +32,6 @@ function ApiDetails(props) {
const setSelectedSampleApi = PersistStore(state => state.setSelectedSampleApi)
const [disabledTabs, setDisabledTabs] = useState([])

const [useLocalSubCategoryData, setUseLocalSubCategoryData] = useState(false)

const statusFunc = getStatus ? getStatus : (x) => {
try {
if (paramList && paramList.length > 0 &&
Expand Down Expand Up @@ -136,13 +130,6 @@ function ApiDetails(props) {
}

useEffect(() => {
if (
(localCategoryMap && Object.keys(localCategoryMap).length > 0) &&
(localSubCategoryMap && Object.keys(localSubCategoryMap).length > 0)
) {
setUseLocalSubCategoryData(true)
}

fetchData();
}, [apiDetail])

Expand Down Expand Up @@ -260,7 +247,7 @@ function ApiDetails(props) {
apiCollectionId={apiDetail["apiCollectionId"]}
endpoints={[apiDetail]}
filtered={true}
useLocalSubCategoryData={useLocalSubCategoryData}
useLocalData={true}
/>
<Box>
<Tooltip content="Open URL in test editor" dismissOnMouseOut>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import func from "@/util/func"
import { useNavigate } from "react-router-dom"
import PersistStore from "../../../../main/PersistStore";
import transform from "../../testing/transform";
import LocalStore from "../../../../main/LocalStorageStore";
import AdvancedSettingsComponent from "./component/AdvancedSettingsComponent";

import {produce} from "immer"
import ObserveStore from "../observeStore";


function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOutside, closeRunTest, selectedResourcesForPrimaryAction, useLocalSubCategoryData }) {
function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOutside, closeRunTest, selectedResourcesForPrimaryAction, useLocalData }) {

const initialState = {
categories: [],
Expand Down Expand Up @@ -68,12 +66,13 @@ function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOu
const [optionsSelected, setOptionsSelected] = useState(initialArr)
const [slackIntegrated, setSlackIntegrated] = useState(false)

const [testAlreadyRunning, setTestAlreadyRunning] = useState(false)

const runTestModalData = useLocalData ? ObserveStore(state => state.runTestModalData) : {}
const setRunTestModalData = ObserveStore(state => state.setRunTestModalData)
const emptyCondition = {data: {key: '', value: ''}, operator: {'type': 'ADD_HEADER'}}
const [conditions, dispatchConditions] = useReducer(produce((draft, action) => func.conditionsReducer(draft, action)), [emptyCondition]);

const localCategoryMap = LocalStore.getState().categoryMap
const localSubCategoryMap = LocalStore.getState().subCategoryMap

function nameSuffixes(tests) {
return Object.entries(tests)
.filter(category => {
Expand All @@ -96,29 +95,45 @@ function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOu

async function fetchData() {
setLoading(true)
let tempRunTestModalData = {}

observeApi.fetchSlackWebhooks().then((resp) => {
const apiTokenList = resp.apiTokenList
setSlackIntegrated(apiTokenList && apiTokenList.length > 0)
})

let metaDataObj = {
categories: [],
subCategories: [],
testSourceConfigs: []
if(runTestModalData.slackIntegrated != null) {
setSlackIntegrated(runTestModalData.slackIntegrated)
} else {
observeApi.fetchSlackWebhooks().then((resp) => {
const apiTokenList = resp.apiTokenList
setSlackIntegrated(apiTokenList && apiTokenList.length > 0)
tempRunTestModalData = {
...tempRunTestModalData,
slackIntegrated: (apiTokenList && apiTokenList.length > 0)
}
})
}
if(!useLocalSubCategoryData) {
metaDataObj = await transform.getAllSubcategoriesData(true, "runTests")

let metaDataObj = {}
if(runTestModalData.metaDataObj != null) {
metaDataObj = runTestModalData.metaDataObj
} else {
metaDataObj = {
categories: Object.values(localCategoryMap),
subCategories: Object.values(localSubCategoryMap),
testSourceConfigs: []
metaDataObj = await transform.getAllSubcategoriesData(true, "runTests")
tempRunTestModalData = {
...tempRunTestModalData,
metaDataObj
}
}
let categories = metaDataObj.categories
let businessLogicSubcategories = metaDataObj.subCategories
const testRolesResponse = await testingApi.fetchTestRoles()


let testRolesResponse
if(runTestModalData.testRolesResponse != null) {
testRolesResponse = runTestModalData.testRolesResponse
} else {
testRolesResponse = await testingApi.fetchTestRoles()
tempRunTestModalData = {
...tempRunTestModalData,
testRolesResponse
}
}
var testRoles = testRolesResponse.testRoles.map(testRole => {
return {
"label": testRole.name,
Expand Down Expand Up @@ -150,7 +165,16 @@ function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOu

//Auth Mechanism
let authMechanismPresent = false
const authMechanismDataResponse = await testingApi.fetchAuthMechanismData()
let authMechanismDataResponse
if(runTestModalData.authMechanismDataResponse != null) {
authMechanismDataResponse = runTestModalData.authMechanismDataResponse
} else {
authMechanismDataResponse = await testingApi.fetchAuthMechanismData()
tempRunTestModalData = {
...tempRunTestModalData,
authMechanismDataResponse
}
}
if (authMechanismDataResponse.authMechanism)
authMechanismPresent = true

Expand All @@ -163,6 +187,10 @@ function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOu
authMechanismPresent: authMechanismPresent
}))

if(tempRunTestModalData != null && Object.keys(tempRunTestModalData).length > 0) {
setRunTestModalData(tempRunTestModalData)
}

setLoading(false)
}

Expand Down Expand Up @@ -413,6 +441,7 @@ function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOu
}

async function handleRun() {
setTestAlreadyRunning(true)
const { startTimestamp, recurringDaily, testName, testRunTime, maxConcurrentRequests, overriddenTestAppUrl, testRoleId, continuousTesting, sendSlackAlert } = testRun
const collectionId = parseInt(apiCollectionId)

Expand Down Expand Up @@ -468,6 +497,7 @@ function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOu
</HorizontalStack>
)

setTestAlreadyRunning(false)
func.setToast(true, false, <div data-testid="test_run_created_message">{forwardLink}</div>)

}
Expand Down Expand Up @@ -530,7 +560,7 @@ function RunTest({ endpoints, filtered, apiCollectionId, disabled, runTestFromOu
primaryAction={{
content: scheduleString(),
onAction: handleRun,
disabled: !testRun.authMechanismPresent
disabled: !testRun.authMechanismPresent || testAlreadyRunning
}}
large
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ let observeStore = (set)=>({
setSelectedUrl:(selectedUrl)=>{
set({selectedUrl: selectedUrl})
},

runTestModalData: {},
setRunTestModalData: (runTestModalData)=>{
set({runTestModalData: runTestModalData})
}
})

observeStore = devtools(observeStore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,33 @@ function TestRoleSettings() {
func.setToast(true, true, "Please select valid values for a test role")
} else {
if (isNew) {
api.addTestRoles(roleName, andConditions, orConditions).then((res) => {
api.addTestRoles(roleName, andConditions, orConditions).then((_) => {
func.setToast(true, false, "Test role added")
setChange(false);
navigate(null, { state: { name: roleName, endpoints: { andConditions: andConditions, orConditions: orConditions } },
replace:true })
}).catch((err) => {
func.setToast(true, true, "Unable to add test role")
}).finally(() => {
setRefresh(!refresh)
})
func.setToast(true, false, "Creating a new test role.")
} else {
await api.updateTestRoles(roleName, andConditions, orConditions).then((res) => {
api.updateTestRoles(roleName, andConditions, orConditions).then((_) => {
if(!updatedAuth){
func.setToast(true, false, "Test role updated successfully.")
}
setChange(false);
navigate(null, { state: { name: roleName, endpoints: { andConditions: andConditions, orConditions: orConditions }, authWithCondList: authWithCondLists || getAuthWithCondList()},
replace:true })
}).catch((err) => {
func.setToast(true, true, "Unable to update test role")
}).finally(() => {
setRefresh(!refresh)
})
if(!updatedAuth){
func.setToast(true, false, "Test role updated successfully.")
}
func.setToast(true, false, "Updating test role.")
}
}
setTimeout(() => {
setRefresh(!refresh)
},200)
}

const handleTextChange = (val) => {
Expand Down
Loading