Tests.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div class="flex flex-col flex-1">
  3. <div
  4. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperMobileSecondaryStickyFold sm:top-upperSecondaryStickyFold"
  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="flex flex-1 border-b border-dividerLight">
  33. <div class="w-2/3 border-r border-dividerLight">
  34. <div ref="testScriptEditor" class="h-full"></div>
  35. </div>
  36. <div
  37. class="sticky h-full p-4 overflow-auto bg-primary top-upperTertiaryStickyFold min-w-46 max-w-1/3 z-9"
  38. >
  39. <div class="pb-2 text-secondaryLight">
  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="pt-6 font-bold text-secondaryLight">
  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. </div>
  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. environmentHighlights: false,
  87. })
  88. )
  89. const useSnippet = (script: string) => {
  90. testScript.value += script
  91. }
  92. const clearContent = () => {
  93. testScript.value = ""
  94. }
  95. </script>