Skip to content

Commit

Permalink
fix(ui): Request Panel > Script > generate test scripts button (#274)…
Browse files Browse the repository at this point in the history
… > updatePlugin doesn't show updated test scripts until you refresh the page
  • Loading branch information
flawiddsouza committed Oct 11, 2024
1 parent a41ee9c commit 36d22ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 45 deletions.
33 changes: 1 addition & 32 deletions packages/ui/src/components/RequestPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1138,38 +1138,7 @@ export default {
async generateTests() {
const generatedTestScripts = await generateTestScripts()
const pluginData = this.$store.state.plugins.workspace.find(plugin =>
plugin.collectionId === this.activeTab._id && plugin.type === 'script'
)
const { pre_request = '', post_request = '' } = pluginData?.code || {}
try {
const updatedPostRequest = `${post_request}\n${generatedTestScripts}`.trim()
const pluginPayload = {
code: {
pre_request,
post_request: updatedPostRequest
},
workspaceId: null,
collectionId: this.activeTab._id,
type: 'script'
}
if (!pluginData) {
this.$store.commit('addPlugin', pluginPayload)
} else {
this.$store.commit('updatePlugin', {
_id: pluginData._id,
code: pluginPayload.code
})
}
this.$toast.success('Test scripts are generated successfully.')
} catch (e) {
this.$toast.error(`Failed to generate test scripts: ${e.message}`)
}
this.script.post_request += generatedTestScripts + `\n`
}
},
mounted() {
Expand Down
28 changes: 15 additions & 13 deletions packages/ui/src/utils/generate-test-scripts.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import dedent from 'dedent'

export async function generateTestScripts() {
return `
test('Status code is 200', () => {
expect(rf.response.getStatusCode()).to.eql(200)
})
return dedent`
test('Status code is 200', () => {
expect(rf.response.getStatusCode()).to.eql(200)
})
test('Response time is less than 500ms', () => {
expect(rf.response.getResponseTime()).to.below(600)
})
test('Response time is less than 500ms', () => {
expect(rf.response.getResponseTime()).to.below(600)
})
test('Body is not empty', () => {
expect(rf.response.getBodyJSON()).to.not.be.empty
})
test('Body is not empty', () => {
expect(rf.response.getBodyJSON()).to.not.be.empty
})
test('headers are not empty', () => {
expect(rf.response.getHeaders()).to.not.be.empty
})
test('headers are not empty', () => {
expect(rf.response.getHeaders()).to.not.be.empty
})
`
}

0 comments on commit 36d22ea

Please sign in to comment.