github.com/DerekStrickland/consul@v1.4.5/ui-v2/tests/helpers/yadda-annotations.js (about) 1 import ENV from '../../config/environment'; 2 import { skip } from 'qunit'; 3 import { setupApplicationTest, setupRenderingTest, setupTest } from 'ember-qunit'; 4 import api from 'consul-ui/tests/helpers/api'; 5 6 const staticClassList = [...document.documentElement.classList]; 7 function reset() { 8 window.localStorage.clear(); 9 api.server.reset(); 10 const list = document.documentElement.classList; 11 while (list.length > 0) { 12 list.remove(list.item(0)); 13 } 14 staticClassList.forEach(function(item) { 15 list.add(item); 16 }); 17 } 18 // this logic could be anything, but in this case... 19 // if @ignore, then return skip (for backwards compatibility) 20 // if have annotations in config, then only run those that have a matching annotation 21 function checkAnnotations(annotations) { 22 // if ignore is set then we want to skip for backwards compatibility 23 if (annotations.ignore) { 24 return ignoreIt; 25 } 26 27 // if have annotations set in config, the only run those that have a matching annotation 28 if (ENV.annotations && ENV.annotations.length >= 0) { 29 for (let annotation in annotations) { 30 if (ENV.annotations.indexOf(annotation) >= 0) { 31 // have match, so test it 32 return 'testIt'; // return something other than a function 33 } 34 } 35 36 // no match, so don't run it 37 return logIt; 38 } 39 } 40 41 // call back functions 42 function ignoreIt(testElement) { 43 skip(`${testElement.title}`, function(/*assert*/) {}); 44 } 45 46 function logIt(testElement) { 47 console.info(`Not running or skipping: "${testElement.title}"`); // eslint-disable-line no-console 48 } 49 50 // exported functions 51 function runFeature(annotations) { 52 return checkAnnotations(annotations); 53 } 54 55 function runScenario(featureAnnotations, scenarioAnnotations) { 56 return checkAnnotations(scenarioAnnotations); 57 } 58 59 // setup tests 60 // you can override these function to add additional setup setups, or handle new setup related annotations 61 function setupFeature(featureAnnotations) { 62 return setupYaddaTest(featureAnnotations); 63 } 64 65 function setupScenario(featureAnnotations, scenarioAnnotations) { 66 let setupFn = setupYaddaTest(scenarioAnnotations); 67 if ( 68 setupFn && 69 (featureAnnotations.setupapplicationtest || 70 featureAnnotations.setuprenderingtest || 71 featureAnnotations.setuptest) 72 ) { 73 throw new Error( 74 'You must not assign any @setupapplicationtest, @setuprenderingtest or @setuptest annotations to a scenario as well as its feature!' 75 ); 76 } 77 return function(model) { 78 model.afterEach(function() { 79 reset(); 80 }); 81 }; 82 // return setupFn; 83 } 84 85 function setupYaddaTest(annotations) { 86 if (annotations.setupapplicationtest) { 87 return setupApplicationTest; 88 } 89 if (annotations.setuprenderingtest) { 90 return setupRenderingTest; 91 } 92 if (annotations.setuptest) { 93 return setupTest; 94 } 95 } 96 97 export { runFeature, runScenario, setupFeature, setupScenario };