UNPKG

4.49 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.object.to-string");
4
5require("core-js/modules/es.promise");
6
7require("regenerator-runtime/runtime");
8
9var _global = require("global");
10
11var _axeCore = _interopRequireDefault(require("axe-core"));
12
13var _addons = _interopRequireDefault(require("@storybook/addons"));
14
15var _constants = require("./constants");
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
18
19function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
20
21function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
22
23if (module && module.hot && module.hot.decline) {
24 module.hot.decline();
25}
26
27var channel = _addons["default"].getChannel(); // Holds axe core running state
28
29
30var active = false; // Holds latest story we requested a run
31
32var activeStoryId;
33
34var getElement = function getElement() {
35 var storyRoot = _global.document.getElementById('story-root');
36
37 return storyRoot ? storyRoot.children : _global.document.getElementById('root');
38};
39/**
40 * Handle A11yContext events.
41 * Because the event are sent without manual check, we split calls
42 */
43
44
45var handleRequest = function handleRequest(storyId) {
46 var _getParams = getParams(storyId),
47 manual = _getParams.manual;
48
49 if (!manual) {
50 run(storyId);
51 }
52};
53
54var run = /*#__PURE__*/function () {
55 var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(storyId) {
56 var input, _input$element, element, config, _input$options, options, result;
57
58 return regeneratorRuntime.wrap(function _callee$(_context) {
59 while (1) {
60 switch (_context.prev = _context.next) {
61 case 0:
62 activeStoryId = storyId;
63 _context.prev = 1;
64 input = getParams(storyId);
65
66 if (active) {
67 _context.next = 13;
68 break;
69 }
70
71 active = true;
72 channel.emit(_constants.EVENTS.RUNNING);
73 _input$element = input.element, element = _input$element === void 0 ? getElement() : _input$element, config = input.config, _input$options = input.options, options = _input$options === void 0 ? {} : _input$options;
74
75 _axeCore["default"].reset();
76
77 if (config) {
78 _axeCore["default"].configure(config);
79 }
80
81 _context.next = 11;
82 return _axeCore["default"].run(element, options);
83
84 case 11:
85 result = _context.sent;
86
87 // It's possible that we requested a new run on a different story.
88 // Unfortunately, axe doesn't support a cancel method to abort current run.
89 // We check if the story we run against is still the current one,
90 // if not, trigger a new run using the current story
91 if (activeStoryId === storyId) {
92 channel.emit(_constants.EVENTS.RESULT, result);
93 } else {
94 active = false;
95 run(activeStoryId);
96 }
97
98 case 13:
99 _context.next = 18;
100 break;
101
102 case 15:
103 _context.prev = 15;
104 _context.t0 = _context["catch"](1);
105 channel.emit(_constants.EVENTS.ERROR, _context.t0);
106
107 case 18:
108 _context.prev = 18;
109 active = false;
110 return _context.finish(18);
111
112 case 21:
113 case "end":
114 return _context.stop();
115 }
116 }
117 }, _callee, null, [[1, 15, 18, 21]]);
118 }));
119
120 return function run(_x) {
121 return _ref.apply(this, arguments);
122 };
123}();
124/** Returns story parameters or default ones. */
125
126
127var getParams = function getParams(storyId) {
128 var _ref2 = _global.window.__STORYBOOK_STORY_STORE__.fromId(storyId) || {},
129 parameters = _ref2.parameters;
130
131 return parameters.a11y || {
132 config: {},
133 options: {
134 restoreScroll: true
135 }
136 };
137};
138
139channel.on(_constants.EVENTS.REQUEST, handleRequest);
140channel.on(_constants.EVENTS.MANUAL, run);
\No newline at end of file