Skip to content
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 fixture pausing with UI controls #89

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/browser/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,18 @@ export async function fixture(element, opts = {}) {
await Promise.all([reset(opts), document.fonts.ready]);
const elem = await wcFixture(element);
await waitForElem(elem, opts.awaitLoadingComplete);

await pause();
return elem;
}

async function pause() {
const test = window.d2lTest || {};

test.update?.();

if (test.pause) {
await test.pause;
if (test.pause) test.pause = new Promise(r => test.run = r);
}
}
4 changes: 3 additions & 1 deletion src/server/headed-mode-plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dirname, join } from 'node:path';
import { globSync } from 'glob';

export function headedMode({ open, watch, pattern }) {
Expand All @@ -8,7 +9,8 @@ export function headedMode({ open, watch, pattern }) {
name: 'brightspace-headed-mode',
async transform(context) {
if ((watch || open) && files.includes(context.path.slice(1))) {
return `debugger;\n${context.body}`;
const pausePath = join(dirname(import.meta.url), 'pause.js').replace('file:', '');
return `debugger;\nimport '${pausePath}'\n${context.body}`;
}
}
};
Expand Down
133 changes: 133 additions & 0 deletions src/server/pause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
const test = window.d2lTest = {};
test.pause = new Promise(r => test.start = r);

const controls = `
<style>
#d2l-test-controls [hidden] {
display: none !important;
}

#d2l-test-controls #start, #run {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these end up in the global namespace, I might suggest prefixing everything with d2l-test-* (i.e. #d2l-test-run, #d2l-test-start) just to avoid the crazy situation where someone uses an id="start" on one of their top-level components.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you mean to prevent them from accidentally selecting the controls? Not entirely opposed to that, but I think I'd rather convert the whole thing to a component after we get it merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah either a getElementById type collision or in this case this CSS would target their stuff and do weird things. But if the plan is to wrap it in a shadow root, then those concerns go away.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this targets stuff inside #d2l-test-controls (except I missed #run here), but point taken.

display: flex;
gap: 1em;
font-size: 18px;
font-family: 'Lato', sans-serif;
padding: 1em;
background: #fff;
color: #222;
margin: calc(-30px + -8px) calc(-30px + -8px) calc(30px + 8px);
flex-wrap: wrap;
align-items: center;
box-shadow: 0 0 5px rgba(0,0,0,.5);
}

#d2l-test-controls #start {
font-size: 24px;
height: 600px;
flex-direction: column;
justify-content: center;
margin-bottom: 0;
box-shadow: none;
}

#d2l-test-controls #test-name {
flex: 1;
}

#d2l-test-controls button {
font-family: 'Lato', sans-serif;
background-color: #e3e9f1;
color: #202122;
border-radius: .3em;
border-style: none;
font-weight: 700;
font-size: .9em;
margin: 0;
min-height: calc(2em + 2px);
outline: none;
padding: .55em 1.5em;
text-align: center;
line-height: 1em;
cursor: pointer;
}

#d2l-test-controls button.primary {
color: #fff;
background-color: #006fbf;
}

#d2l-test-controls button:focus {
background-color: #cdd5dc;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks duplicated from lines 54-56?


#d2l-test-controls button:focus-visible {
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #006fbf;
}

#d2l-test-controls button.primary:focus {
background-color: #004489;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional?
Screenshot 2023-07-24 at 2 44 46 PM

Suggest either always having the background color set on "primary" buttons and also setting the foreground to white, or just not having the concept of primary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see this was resolved in a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah things are slightly messy trying to piece it all back together from the original big PR.

}

#d2l-test-controls button[disabled] {
cursor: default;
opacity: 0.5;
bearfriend marked this conversation as resolved.
Show resolved Hide resolved
}
</style>
<div id="d2l-test-controls">
<div id="start">
<span>.${window.__WTR_CONFIG__.testFile.split('?')[0]}</span>
<button id="start-button" class="primary">Start</button>
</div>
<div id="run" hidden>
<button id="run-button" class="primary">Run</button>
<span id="test-name"></span>
<button id="run-all-button">Run All</button>
</div>
</div>
`;

document.body.insertAdjacentHTML('afterBegin', controls);

const startBtn = document.querySelector('#start-button');
startBtn.addEventListener('click', start);

const runAllBtn = document.querySelector('#run-all-button');
runAllBtn.addEventListener('click', runAll);

const runBtn = document.querySelector('#run-button');
runBtn.addEventListener('click', run);

const testName = document.querySelector('#test-name');

beforeEach(async function() { // eslint-disable-line no-undef
const fixture = new Promise(r => test.update = r);
const currentTest = this.currentTest; // eslint-disable-line no-invalid-this
setTimeout(async() => {
await fixture;
testName.innerText = currentTest.fullTitle();
if (test.pause) {
runBtn.disabled = false;
runBtn.focus();
}
});
});

function start() {
document.querySelector('#start').hidden = true;
document.querySelector('#run').hidden = false;
test.start();
}

function run() {
runBtn.disabled = true;
test.run();
}

function runAll() {
runAllBtn.disabled = true;
test.pause = null;
run();
}

await test.pause;
test.pause = new Promise(r => test.run = r);