Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use font-families endpoint to check installed fonts
  • Loading branch information
mikachan committed Feb 27, 2024
commit 287252a802d75d6861ae00160ccf367f37165127
22 changes: 12 additions & 10 deletions test/e2e/specs/site-editor/font-library.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,24 @@ test.describe( 'Font Library', () => {

test( 'should display the "Upload" tab and upload a local font', async ( {
page,
request,
} ) => {
await expect(
page.getByRole( 'tab', { name: 'Upload' } )
).toBeVisible( { timeout: 40000 } );
Copy link
Contributor

Choose a reason for hiding this comment

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

What condition is applying here in the UI to cause you to need such a long timeout?

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems to take quite a while to fetch the font family post data, it looks like it's the useEntityRecords call here that takes a while to resolve. There doesn't seem to be any errors, just that it takes longer than the default timeout set in Playwight to resolve.

I'm currently looking at adding logic that would wait for this to resolve before running any of these tests. This should remove the need for specific timeout flags, but I'm assuming realistically it's still going to be a slower-than-average suite of tests, which I'm not a fan of adding.

Copy link
Contributor

Choose a reason for hiding this comment

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

In which case I would be in favour of adding perhaps 1 or 2 high level "smoke tests". We might not catch every single bug but it might be enough to guard regressions around critical functionality (e.g. whether or not we display the Upload tab to users for example).

Copy link
Contributor

Choose a reason for hiding this comment

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

What's the minimum set of tests we could introduce? Or are we already at that point?

Copy link
Member Author

@mikachan mikachan Feb 28, 2024

Choose a reason for hiding this comment

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

In which case I would be in favour of adding perhaps 1 or 2 high level "smoke tests".

I've been thinking this too, I'll update the PR so the tests don't include uploading a font. We can always come back to that later if necessary. Also, I learned how to upload files in Playwright, so that's something 😄

What's the minimum set of tests we could introduce? Or are we already at that point?

I like the idea of testing whether the Upload tab should appear or not based on permissions, so perhaps these are enough for now. It doesn't just test the permissions but also whether the font face posts have been initialised/fetched correctly, as the tab will only show once the posts have been fetched.

Copy link
Member Author

Choose a reason for hiding this comment

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

An installed and activated font is shown correctly in the list of Font Library fonts
The Google Fonts collection has a tab shown in the Font Library modal

These were covered previously but I've just removed them to simplify these tests, but happy to add them back! The main issue I'm seeing is that resolving the font family posts takes a few extra seconds, so this means we'd be adding some potentially slow e2e tests. I'm wondering if there's something we can improve here in the order in which the font posts are requested, i.e. perhaps the request could start at the same time as the modal opening, rather than after the modal has opened.

But using custom permissions seems like a rare use case (I'd guess a small fraction of a percent of sites), so still not sure it's worth spending an e2e test on.

Maybe we should test whether the Font Library UI is disabled via the editor settings rather than checking permissions. Does that sound worthy of a test?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should test whether the Font Library UI is disabled via the editor settings rather than checking permissions. Does that sound worthy of a test?

Yes, I think it would be.

Copy link
Contributor

Choose a reason for hiding this comment

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

The main issue I'm seeing is that resolving the font family posts takes a few extra seconds, so this means we'd be adding some potentially slow e2e tests.

Not sure if this is a good idea, but it should be possible to preload the font-families API response using a filter, in the same way that essential routes that are used when the editor first loads are preloaded, by outputting the response JSON into the html markup on the page when it loads.

But I don't think this would make sense normally, where the font library may or may not be opened. It would only be for the test environment--hence not being sure if it's a good idea to "fork" the test environment like this. But it may be worth it--the only change is where the response data comes from, the actual API response should be exactly the same either way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just eliminate the default font collection from the tests? Unregister that one and register another, much smaller one that won't require a json file to resolve at all. We'll probably wind up with a few different font collection scenarios to manufacture and test anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if this is a good idea, but it should be possible to preload the font-families API response using a filter

There are a few instances in the e2e tests where we wait for either certain blocks to load in or specific endpoints to resolve before moving on, so I think we can use a similar method here.

Can we just eliminate the default font collection from the tests?

The default font collection isn't loaded until the Google Fonts acceptance notice has been clicked, which hasn't been tested so far, so the whole collection hasn't been resolved as part of these tests, only the tab.

It's the initial post request that seems to take a while, but I'm not sure what part of it is taking a while or if it's a sign that performance needs to be improved somewhere. I usually find it easy to replicate in a Playground instance too - the initial request takes a noticeable 5-8 seconds. I'm not sure if addressing this is an urgent priority, and initially, perhaps we could look at improving the UX around it. Currently, there is a spinner that appears during the post request and then disappears once resolved. If there are no custom fonts, it's not clear what the spinner is indicating.

Also apologies as I think I'm deviating a bit here 😅 I plan to finish up the minimal tests in this PR and maybe we should open a separate issue to discuss performance improvements.


// Ensure font does not exist before uploading
if (
await page
.getByRole( 'button', { name: 'Commissioner' } )
.isVisible( { timeout: 60000 } )
) {
await page
.getByRole( 'button', { name: 'Commissioner' } )
.click();
await page.getByRole( 'button', { name: 'Delete' } ).click();
await page.getByRole( 'button', { name: 'Delete' } ).click();
const installedFonts = await request.get(
`/wp-json/wp/v2/font-families`
);
console.log( installedFonts );
const commissionerFont = await request.get(
`/wp-json/wp/v2/font-families?slug=commissioner&_embed=true`
);
if ( commissionerFont.length > 0 ) {
await request.delete(
`/wp-json/wp/v2/font-families/${ commissionerFont[ 0 ].id }?force=true`
);
}

// Upload a local font
Expand Down