Tests.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <AppSection id="script" :label="`${t('test.script')}`">
  3. <div
  4. class="bg-primary border-b border-dividerLight flex flex-1 top-upperSecondaryStickyFold pl-4 z-10 sticky items-center justify-between"
  5. >
  6. <label class="font-semibold text-secondaryLight">
  7. {{ t("test.javascript_code") }}
  8. </label>
  9. <div class="flex">
  10. <ButtonSecondary
  11. v-tippy="{ theme: 'tooltip' }"
  12. to="https://docs.hoppscotch.io/features/tests"
  13. blank
  14. :title="t('app.wiki')"
  15. svg="help-circle"
  16. />
  17. <ButtonSecondary
  18. v-tippy="{ theme: 'tooltip' }"
  19. :title="t('state.linewrap')"
  20. :class="{ '!text-accent': linewrapEnabled }"
  21. svg="wrap-text"
  22. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  23. />
  24. <ButtonSecondary
  25. v-tippy="{ theme: 'tooltip' }"
  26. :title="t('action.clear')"
  27. svg="trash-2"
  28. @click.native="clearContent"
  29. />
  30. </div>
  31. </div>
  32. <div class="border-b border-dividerLight flex">
  33. <div class="border-r border-dividerLight w-2/3">
  34. <div ref="testScriptEditor"></div>
  35. </div>
  36. <div
  37. class="bg-primary h-full top-upperTertiaryStickyFold min-w-46 max-w-1/3 p-4 z-9 sticky overflow-auto"
  38. >
  39. <div class="text-secondaryLight pb-2">
  40. {{ t("helpers.post_request_tests") }}
  41. </div>
  42. <SmartAnchor
  43. :label="`${t('test.learn')}`"
  44. to="https://docs.hoppscotch.io/features/tests"
  45. blank
  46. />
  47. <h4 class="font-bold text-secondaryLight pt-6">
  48. {{ t("test.snippets") }}
  49. </h4>
  50. <div class="flex flex-col pt-4">
  51. <TabSecondary
  52. v-for="(snippet, index) in testSnippets"
  53. :key="`snippet-${index}`"
  54. :label="snippet.name"
  55. active
  56. @click.native="useSnippet(snippet.script)"
  57. />
  58. </div>
  59. </div>
  60. </div>
  61. </AppSection>
  62. </template>
  63. <script setup lang="ts">
  64. import { reactive, ref } from "@nuxtjs/composition-api"
  65. import { useTestScript } from "~/newstore/RESTSession"
  66. import testSnippets from "~/helpers/testSnippets"
  67. import { useCodemirror } from "~/helpers/editor/codemirror"
  68. import linter from "~/helpers/editor/linting/testScript"
  69. import completer from "~/helpers/editor/completion/testScript"
  70. import { useI18n } from "~/helpers/utils/composables"
  71. const t = useI18n()
  72. const testScript = useTestScript()
  73. const testScriptEditor = ref<any | null>(null)
  74. const linewrapEnabled = ref(true)
  75. useCodemirror(
  76. testScriptEditor,
  77. testScript,
  78. reactive({
  79. extendedEditorConfig: {
  80. mode: "application/javascript",
  81. lineWrapping: linewrapEnabled,
  82. placeholder: `${t("test.javascript_code")}`,
  83. },
  84. linter,
  85. completer,
  86. })
  87. )
  88. const useSnippet = (script: string) => {
  89. testScript.value += script
  90. }
  91. const clearContent = () => {
  92. testScript.value = ""
  93. }
  94. </script>