Skip to content

Instantly share code, notes, and snippets.

@zeppelin
Created April 17, 2019 19:48
Show Gist options
  • Save zeppelin/87be11387bda58456c835f86a28dae8c to your computer and use it in GitHub Desktop.
Save zeppelin/87be11387bda58456c835f86a28dae8c to your computer and use it in GitHub Desktop.
Conventional test selectors
test('createTestSelector', function(assert) {
// Base namespace could be, for example, the component's name
let base = createTestSelector('namespace');
// Element that appears only once on the screen at a time
assert.equal(base('single'), '[data-test-namespace="single"]');
// List of elements, targeting all items
assert.equal(base('multiple', null), '[data-test-namespace^="multiple:"]');
// List of elements, targeting a single item
assert.equal(base('multiple', 'unique'), '[data-test-namespace="multiple:unique"]');
});
// Impl.
const createTestSelector = (namespace: string) => (item: string, identifier?: string | null): string => {
let operator = '';
if (identifier) {
item = `${item}:${identifier}`;
} else if (identifier === null) {
operator = '^';
item = `${item}:`;
}
return `[data-test-${namespace}${operator}="${item}"]`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment