From 19f3b6e954177b023bdcc872d564fc4a580874ca Mon Sep 17 00:00:00 2001 From: zombiej Date: Sat, 1 Dec 2018 12:24:57 +0800 Subject: [PATCH] add aria wrapper --- tests/shared/demoTest.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/shared/demoTest.js b/tests/shared/demoTest.js index 09979dd3c4..d086b2ce2f 100644 --- a/tests/shared/demoTest.js +++ b/tests/shared/demoTest.js @@ -3,6 +3,42 @@ import { render } from 'enzyme'; import MockDate from 'mockdate'; import moment from 'moment'; +const testDist = process.env.LIB_DIR === 'dist'; + +/** + * rc component will generate id for aria usage. + * It's created as `test-uuid` when env === 'test'. + * Or `f7fa7a3c-a675-47bc-912e-0c45fb6a74d9`(randomly) when not test env. + * So we need hack of this to modify the `aria-controls`. + */ +function ariaConvert(wrapper) { + if (!testDist) return wrapper; + + const matches = new Map(); + + function process(entry) { + const { attribs, children } = entry; + if (matches.has(entry)) return; + matches.set(entry, true); + + // Change aria + if (attribs && attribs['aria-controls']) { + attribs['aria-controls'] = 'test-uuid'; + } + + // Loop children + if (!children) return; + (Array.isArray(children) ? children : [children]).forEach(process); + } + + Object.keys(wrapper).forEach((key) => { + const entry = wrapper[key]; + process(entry); + }); + + return wrapper; +} + export default function demoTest(component, options = {}) { const files = glob.sync(`./components/${component}/demo/*.md`); @@ -15,6 +51,10 @@ export default function demoTest(component, options = {}) { MockDate.set(moment('2016-11-22')); const demo = require(`../.${file}`).default; // eslint-disable-line global-require, import/no-dynamic-require const wrapper = render(demo); + + // Convert aria related content + ariaConvert(wrapper); + expect(wrapper).toMatchSnapshot(); MockDate.reset(); });