Created
April 17, 2019 19:48
-
-
Save zeppelin/87be11387bda58456c835f86a28dae8c to your computer and use it in GitHub Desktop.
Conventional test selectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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