vault/ui/tests/unit/utils/api-error-message-test.js
Jordan Reimer 85afe0c3a3
[UI] Ember Data Migration - Tools Wrap Component (#29994)
* adds codemirror types

* adds api error message util

* converts tools/wrap component to ts and updates wrap request to use api service

* adds comments to api and error message utils

* adds type to html element event import
2025-03-24 10:49:48 -06:00

52 lines
1.9 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import apiErrorMessage from 'vault/utils/api-error-message';
module('Unit | Util | api-error-message', function (hooks) {
hooks.beforeEach(function () {
this.apiError = {
errors: ['first error', 'second error'],
message: 'there were some errors',
};
this.getErrorContext = () => ({ response: new Response(JSON.stringify(this.apiError)) });
});
test('it should return errors from ErrorContext', async function (assert) {
const message = await apiErrorMessage(this.getErrorContext());
assert.strictEqual(message, 'first error, second error');
});
test('it should return message from ErrorContext when errors are empty', async function (assert) {
this.apiError.errors = [];
const message = await apiErrorMessage(this.getErrorContext());
assert.strictEqual(message, 'there were some errors');
});
test('it should return fallback message for ErrorContext without errors or message', async function (assert) {
this.apiError = {};
const message = await apiErrorMessage(this.getErrorContext());
assert.strictEqual(message, 'An error occurred, please try again');
});
test('it should return message from Error', async function (assert) {
const error = new Error('some js type error');
const message = await apiErrorMessage(error);
assert.strictEqual(message, error.message);
});
test('it should return default fallback', async function (assert) {
const message = await apiErrorMessage('some random error');
assert.strictEqual(message, 'An error occurred, please try again');
});
test('it should return custom fallback message', async function (assert) {
const fallback = 'Everything is broken, sorry';
const message = await apiErrorMessage('some random error', fallback);
assert.strictEqual(message, fallback);
});
});