-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add abstraction logic for simplifying writing visual tests #685
Merged
AlexVelezLl
merged 15 commits into
learningequality:gsoc/visual-testing
from
KshitijThareja:vt-abstraction
Jul 26, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
65a08d4
Introduce abstraction for component rendering functions and support f…
KshitijThareja d2ea3d1
Initial workflow config
KshitijThareja 8ce5fe8
Fix the initial workflow for visual tests job
KshitijThareja 2eaabdb
Add automatic comments job for visual testing workflow
KshitijThareja 310f75b
Fix lint errors
KshitijThareja f6c66f3
Abstraction for page navigation
KshitijThareja 1eba82f
Fix lint errors
KshitijThareja c37ddd3
Merge branch 'gsoc/visual-testing' into vt-abstraction
KshitijThareja f4373a0
Introduce visual testing to the existing Javascript tests workflow
KshitijThareja bdea16d
Introduce visual testing to the existing Javascript tests workflow
KshitijThareja c0e977f
Replace usage of custom describe blocks with test regex patterns
KshitijThareja 95228a2
Merge branch 'vt-abstraction' of github.com:KshitijThareja/kolibri-de…
KshitijThareja 61c98f9
Remove unintentional changes
KshitijThareja fde97db
Add comments for rendering function and make variable names clear
KshitijThareja 93f2b7c
Add special test blocks for visual tests to append Visual tag automat…
KshitijThareja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ jobs: | |
yarn --frozen-lockfile | ||
npm rebuild node-sass | ||
- name: Run tests | ||
run: yarn test | ||
run: yarn test |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import percySnapshot from '@percy/puppeteer'; | ||
|
||
export async function renderComponent(component, props) { | ||
const beforeRenderState = await page.evaluate(() => { | ||
const testing_playground = document.querySelector('#testing-playground'); | ||
return testing_playground ? testing_playground.innerHTML : ''; | ||
}); | ||
|
||
await page.evaluate( | ||
({ component, props }) => { | ||
window.postMessage( | ||
{ | ||
type: 'RENDER_COMPONENT', | ||
component: component, | ||
props: props, | ||
}, | ||
'*' | ||
); | ||
}, | ||
{ component, props } | ||
); | ||
await page.waitForSelector('#testing-playground'); | ||
|
||
// Wait until the innerHTML of the testing playground changes, indicating that the component has been rendered. | ||
await page.waitForFunction( | ||
AlexVelezLl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
initialState => { | ||
const testing_playground = document.querySelector('#testing-playground'); | ||
return testing_playground && testing_playground.innerHTML !== initialState; | ||
}, | ||
{}, | ||
beforeRenderState | ||
); | ||
|
||
// Check if the component has been rendered by comparing the initial state with the current state. | ||
const isComponentRendered = await page.evaluate(initialState => { | ||
const testing_playground = document.querySelector('#testing-playground'); | ||
return testing_playground && testing_playground.innerHTML !== initialState; | ||
}, beforeRenderState); | ||
|
||
global.expect(isComponentRendered).toBe(true); | ||
} | ||
|
||
export async function takeSnapshot(name) { | ||
if (process.env.TEST_TYPE == 'visual') { | ||
await percySnapshot(page, name); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,75 @@ | ||
import { shallowMount } from '@vue/test-utils'; | ||
import percySnapshot from '@percy/puppeteer'; | ||
import KButton from '../KButton.vue'; | ||
import { canTakeScreenshot } from '../../../jest.conf/testUtils'; | ||
import { renderComponent, takeSnapshot } from '../../../jest.conf/visual.testUtils'; | ||
|
||
describe('KButton', () => { | ||
if (!canTakeScreenshot()) { | ||
describe('icon related props', () => { | ||
it('should render an icon before the text with the icon string passed to the `icon` prop', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
icon: 'add', | ||
}, | ||
}); | ||
expect(wrapper.find('[data-test="iconBefore"]').exists()).toBe(true); | ||
describe('icon related props', () => { | ||
it('should render an icon before the text with the icon string passed to the `icon` prop', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
icon: 'add', | ||
}, | ||
}); | ||
it('should render an icon after the text with the icon string pased to the `iconAfter` prop', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
iconAfter: 'video', | ||
}, | ||
}); | ||
expect(wrapper.find('[data-test="iconAfter"]').exists()).toBe(true); | ||
expect(wrapper.find('[data-test="iconBefore"]').exists()).toBe(true); | ||
}); | ||
it('should render an icon after the text with the icon string pased to the `iconAfter` prop', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
iconAfter: 'video', | ||
}, | ||
}); | ||
it('should render a dropdown icon when hasDropdown is true', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
hasDropdown: true, | ||
}, | ||
}); | ||
expect(wrapper.find('[data-test="dropdownIcon"]').exists()).toBe(true); | ||
expect(wrapper.find('[data-test="iconAfter"]').exists()).toBe(true); | ||
}); | ||
it('should render a dropdown icon when hasDropdown is true', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
hasDropdown: true, | ||
}, | ||
}); | ||
expect(wrapper.find('[data-test="dropdownIcon"]').exists()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('text prop and slots', () => { | ||
it('should render the text prop if nothing is in the default slot', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
text: 'test', | ||
}, | ||
}); | ||
expect(wrapper.text()).toContain('test'); | ||
describe('text prop and slots', () => { | ||
it('should render the text prop if nothing is in the default slot', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
text: 'test', | ||
}, | ||
}); | ||
expect(wrapper.text()).toContain('test'); | ||
}); | ||
|
||
it('should render the slot when the slot has content', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
text: 'test', | ||
}, | ||
slots: { | ||
default: '<span>slot</span>', | ||
}, | ||
}); | ||
expect(wrapper.text()).toContain('slot'); | ||
expect(wrapper.text()).toContain('test'); | ||
it('should render the slot when the slot has content', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
text: 'test', | ||
}, | ||
slots: { | ||
default: '<span>slot</span>', | ||
}, | ||
}); | ||
expect(wrapper.text()).toContain('slot'); | ||
expect(wrapper.text()).toContain('test'); | ||
}); | ||
}); | ||
|
||
describe('event handling', () => { | ||
it('should emit a click event when clicked', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
text: 'test', | ||
}, | ||
}); | ||
wrapper.trigger('click'); | ||
expect(wrapper.emitted().click).toBeTruthy(); | ||
describe('event handling', () => { | ||
it('should emit a click event when clicked', () => { | ||
const wrapper = shallowMount(KButton, { | ||
propsData: { | ||
text: 'test', | ||
}, | ||
}); | ||
wrapper.trigger('click'); | ||
expect(wrapper.emitted().click).toBeTruthy(); | ||
}); | ||
} else { | ||
describe('KButton Visual Tests', () => { | ||
beforeAll(async () => { | ||
await page.goto('http://localhost:4000/testing-playground', { waitUntil: 'networkidle2' }); | ||
}); | ||
}); | ||
|
||
async function renderComponent(component, props) { | ||
await page.evaluate( | ||
({ component, props }) => { | ||
window.postMessage( | ||
{ | ||
type: 'RENDER_COMPONENT', | ||
component: component, | ||
props: props, | ||
}, | ||
'*' | ||
); | ||
}, | ||
{ component, props } | ||
); | ||
await page.waitForSelector('#testing-playground'); | ||
|
||
const isComponentRendered = await page.evaluate(() => { | ||
const testing_playground = document.querySelector('#testing-playground'); | ||
return testing_playground && testing_playground.children.length > 0; | ||
}); | ||
|
||
if (!isComponentRendered) { | ||
// eslint-disable-next-line no-console | ||
console.error('Component did not render in the testing playground'); | ||
} | ||
} | ||
|
||
it('renders correctly with default props', async () => { | ||
await renderComponent('KButton', { text: 'Test Button' }); | ||
await percySnapshot(page, 'KButton - Default'); | ||
}); | ||
describe.visual('KButton Visual Tests', () => { | ||
it('renders correctly with default props', async () => { | ||
AlexVelezLl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await renderComponent('KButton', { text: 'Test Button' }); | ||
await takeSnapshot('KButton - Default'); | ||
}); | ||
} | ||
}); | ||
}); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wooo, thats great, I researched if we had something like this for jest.config but I found nothing in the docs, but its great that this is working 🎉