2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2020-01-24 12:26:28 +00:00
|
|
|
import { skip, test } from 'qunit';
|
|
|
|
import { setupApplicationTest } from 'ember-qunit';
|
|
|
|
import Yadda from 'yadda';
|
|
|
|
|
|
|
|
import { env } from '../../env';
|
|
|
|
import api from './api';
|
2020-10-23 16:26:06 +00:00
|
|
|
import utils from './page';
|
|
|
|
import dictionary from '../dictionary';
|
|
|
|
const getDictionary = dictionary(utils);
|
2018-05-11 12:47:21 +00:00
|
|
|
|
2018-06-25 11:11:01 +00:00
|
|
|
const staticClassList = [...document.documentElement.classList];
|
2020-12-15 15:34:54 +00:00
|
|
|
const getCookies = () => {
|
2022-09-15 08:43:17 +00:00
|
|
|
return Object.fromEntries(document.cookie.split(';').map((item) => item.split('=')));
|
2020-12-15 15:34:54 +00:00
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const getResetCookies = function () {
|
2020-12-15 15:34:54 +00:00
|
|
|
const start = getCookies();
|
|
|
|
return () => {
|
|
|
|
const startKeys = Object.keys(start);
|
|
|
|
const endKeys = Object.keys(getCookies());
|
2022-09-15 08:43:17 +00:00
|
|
|
const diff = endKeys.filter((key) => !startKeys.includes(key));
|
|
|
|
diff.forEach((item) => {
|
2020-12-15 15:34:54 +00:00
|
|
|
document.cookie = `${item}= ; expires=${new Date(0)}`;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let resetCookies;
|
2022-09-15 08:43:17 +00:00
|
|
|
const reset = function () {
|
2020-12-15 15:34:54 +00:00
|
|
|
resetCookies();
|
2018-06-25 11:11:01 +00:00
|
|
|
window.localStorage.clear();
|
|
|
|
api.server.reset();
|
|
|
|
const list = document.documentElement.classList;
|
|
|
|
while (list.length > 0) {
|
|
|
|
list.remove(list.item(0));
|
|
|
|
}
|
2022-09-15 08:43:17 +00:00
|
|
|
staticClassList.forEach(function (item) {
|
2018-06-25 11:11:01 +00:00
|
|
|
list.add(item);
|
|
|
|
});
|
2020-01-24 12:26:28 +00:00
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const startup = function () {
|
2020-12-15 15:34:54 +00:00
|
|
|
resetCookies = getResetCookies();
|
2020-09-09 17:51:18 +00:00
|
|
|
api.server.setCookie('CONSUL_LATENCY', 0);
|
|
|
|
};
|
2018-05-11 12:47:21 +00:00
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
const runTest = function (context, libraries, steps, scenarioContext) {
|
2020-01-24 12:26:28 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Yadda.Yadda(libraries, context).yadda(steps, scenarioContext, function next(err, result) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
2018-05-11 12:47:21 +00:00
|
|
|
}
|
2020-01-24 12:26:28 +00:00
|
|
|
resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const checkAnnotations = function (annotations, isScenario) {
|
2020-01-24 12:26:28 +00:00
|
|
|
annotations = {
|
2020-02-07 11:02:53 +00:00
|
|
|
namespaceable: env('CONSUL_NSPACES_ENABLED'),
|
2020-01-24 12:26:28 +00:00
|
|
|
...annotations,
|
|
|
|
};
|
|
|
|
if (annotations.ignore) {
|
2022-09-15 08:43:17 +00:00
|
|
|
return function (test) {
|
|
|
|
skip(`${test.title}`, function (assert) {});
|
2020-01-24 12:26:28 +00:00
|
|
|
};
|
2018-05-11 12:47:21 +00:00
|
|
|
}
|
2020-01-24 12:26:28 +00:00
|
|
|
if (isScenario) {
|
2020-02-07 11:02:53 +00:00
|
|
|
if (env('CONSUL_NSPACES_ENABLED')) {
|
|
|
|
if (!annotations.notnamespaceable) {
|
2022-09-15 08:43:17 +00:00
|
|
|
return function (scenario, feature, yadda, yaddaAnnotations, library) {
|
2020-10-23 16:26:06 +00:00
|
|
|
const stepDefinitions = library.default;
|
2022-09-15 08:43:17 +00:00
|
|
|
['', 'default', 'team-1', undefined].forEach(function (item) {
|
2020-02-07 11:02:53 +00:00
|
|
|
test(`Scenario: ${
|
|
|
|
scenario.title
|
2022-09-15 08:43:17 +00:00
|
|
|
} with the ${item === '' ? 'empty' : typeof item === 'undefined' ? 'undefined' : item} namespace set`, function (assert) {
|
2020-02-07 11:02:53 +00:00
|
|
|
const scenarioContext = {
|
|
|
|
ctx: {
|
|
|
|
nspace: item,
|
|
|
|
},
|
|
|
|
};
|
2020-10-23 16:26:06 +00:00
|
|
|
const libraries = stepDefinitions({
|
|
|
|
assert: assert,
|
|
|
|
utils: utils,
|
|
|
|
library: Yadda.localisation.English.library(getDictionary(annotations, item)),
|
|
|
|
});
|
|
|
|
return runTest(this, libraries, scenario.steps, scenarioContext);
|
2020-02-07 11:02:53 +00:00
|
|
|
});
|
|
|
|
});
|
2020-01-24 12:26:28 +00:00
|
|
|
};
|
2020-02-07 11:02:53 +00:00
|
|
|
} else {
|
2022-09-15 08:43:17 +00:00
|
|
|
return function () {};
|
2020-02-07 11:02:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!annotations.onlynamespaceable) {
|
2022-09-15 08:43:17 +00:00
|
|
|
return function (scenario, feature, yadda, yaddaAnnotations, library) {
|
2020-10-23 16:26:06 +00:00
|
|
|
const stepDefinitions = library.default;
|
2022-09-15 08:43:17 +00:00
|
|
|
test(`Scenario: ${scenario.title}`, function (assert) {
|
2020-01-24 12:26:28 +00:00
|
|
|
const scenarioContext = {
|
2020-02-07 11:02:53 +00:00
|
|
|
ctx: {},
|
2020-01-24 12:26:28 +00:00
|
|
|
};
|
2020-10-23 16:26:06 +00:00
|
|
|
const libraries = stepDefinitions({
|
|
|
|
assert: assert,
|
|
|
|
utils: utils,
|
|
|
|
library: Yadda.localisation.English.library(getDictionary(annotations)),
|
|
|
|
});
|
2020-01-24 12:26:28 +00:00
|
|
|
return runTest(this, libraries, scenario.steps, scenarioContext);
|
|
|
|
});
|
2020-02-07 11:02:53 +00:00
|
|
|
};
|
|
|
|
} else {
|
2022-09-15 08:43:17 +00:00
|
|
|
return function () {};
|
2020-01-24 12:26:28 +00:00
|
|
|
}
|
2020-02-07 11:02:53 +00:00
|
|
|
}
|
2018-05-11 12:47:21 +00:00
|
|
|
}
|
2020-01-24 12:26:28 +00:00
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const setupFeature = function (featureAnnotations) {
|
2020-01-24 12:26:28 +00:00
|
|
|
return setupApplicationTest;
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const setupScenario = function (featureAnnotations, scenarioAnnotations) {
|
|
|
|
return function (model) {
|
|
|
|
model.beforeEach(function () {
|
2020-09-09 17:51:18 +00:00
|
|
|
startup();
|
|
|
|
});
|
2022-09-15 08:43:17 +00:00
|
|
|
model.afterEach(function () {
|
2018-06-25 11:11:01 +00:00
|
|
|
reset();
|
2018-05-11 12:47:21 +00:00
|
|
|
});
|
|
|
|
};
|
2020-01-24 12:26:28 +00:00
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const runFeature = function (annotations) {
|
2020-01-24 12:26:28 +00:00
|
|
|
return checkAnnotations(annotations);
|
|
|
|
};
|
2018-05-11 12:47:21 +00:00
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
export const runScenario = function (featureAnnotations, scenarioAnnotations) {
|
2020-01-24 12:26:28 +00:00
|
|
|
return checkAnnotations({ ...featureAnnotations, ...scenarioAnnotations }, true);
|
|
|
|
};
|