Skip to content

Commit acde4f9

Browse files
fix: google fonts component and related issues, add E2E (#3531)
* fix: google styles were not applied * fix: update array type form script error * fix: computing descendants * fix: e2e to work with component instances in builder tree * fix: google fonts issues and add E2E tests * fix: failing e2e
1 parent 2c5261e commit acde4f9

File tree

21 files changed

+344
-152
lines changed

21 files changed

+344
-152
lines changed

apps/web-e2e/src/modules/builder/builder.fixture.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ export class BuilderPage extends BasePage {
105105
await this.getModal().locator(submitButton).click()
106106

107107
await expect(this.getModal()).toBeHidden()
108-
await expect(
109-
this.getTreeElement(name, atom).or(
110-
this.getTreeElement(name, `instance of ${atom}`),
111-
),
112-
).toBeVisible()
108+
await expect(this.getTreeElement(name, atom)).toBeVisible()
113109
}
114110
}
115111

@@ -186,7 +182,9 @@ export class BuilderPage extends BasePage {
186182
}
187183

188184
getTreeElement(name: string, type?: string) {
189-
return this.page.getByTitle(`${name} (${type ?? name})`)
185+
return this.page
186+
.getByTitle(`${name} (${type ?? name})`)
187+
.or(this.page.getByTitle(`${name} (instance of ${type ?? name})`))
190188
}
191189

192190
getUpdateElementForm() {
@@ -242,7 +240,9 @@ export class BuilderPage extends BasePage {
242240

243241
await treeElement.click()
244242

245-
await expect(selectedTreeElement).toHaveText(`${name}${atom}`)
243+
await expect(selectedTreeElement).toHaveText(
244+
new RegExp(`^(${name}${atom}|${name}instance of ${atom})$`),
245+
)
246246
await expect(this.getFormFieldSpinner()).toHaveCount(0)
247247

248248
return treeElement

apps/web-e2e/src/modules/css/css.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ test.beforeEach(async ({ builderPage: page }) => {
2525
await page.checkPageHeaderTitle(['Codelab App', 'Pages', 'provider'])
2626

2727
await expect(page.getSpinner()).toBeHidden()
28+
await expect(page.getFormFieldSpinner()).toHaveCount(0)
2829
})
2930

3031
test('should be able to set styling through css string and GUI', async ({
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
IAtomType,
3+
type ICreateCypressElementData,
4+
ITypeKind,
5+
} from '@codelab/shared/abstract/core'
6+
import { ROOT_ELEMENT_NAME } from '@codelab/shared/config'
7+
import { type APIRequestContext } from '@playwright/test'
8+
import { v4 } from 'uuid'
9+
10+
import { seedAppData } from '../builder/builder.data'
11+
12+
export const FONT_NAME = 'Google Fonts Montserrat'
13+
export const FONT_SIZE = 700
14+
15+
export const googleFontsElement: ICreateCypressElementData = {
16+
atom: 'Google Fonts',
17+
name: 'Google Fonts Link',
18+
parentElement: ROOT_ELEMENT_NAME,
19+
}
20+
21+
export const typographyElement = {
22+
atom: IAtomType.AntDesignTypographyText,
23+
id: v4(),
24+
name: 'Text Element',
25+
propsData: {
26+
children: {
27+
kind: ITypeKind.RichTextType,
28+
type: 'e7558508-3bb7-4f57-8f8c-6ac989911765',
29+
value: 'Testing fonts',
30+
},
31+
},
32+
}
33+
34+
export const seedTestData = async (request: APIRequestContext) => {
35+
const app = await seedAppData(request)
36+
const page = app.pages![0]!
37+
38+
await request.post('/api/v1/component/seed-system-components')
39+
await request.post(`/api/v1/element/${page.rootElement.id}/create-elements`, {
40+
data: [{ ...typographyElement, parentElement: page.rootElement }],
41+
})
42+
43+
return app
44+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { Locator } from '@playwright/test'
2+
3+
import { test as base, expect } from '@playwright/test'
4+
5+
import { CssBuilderPage } from '../css/css.fixture'
6+
import { FONT_NAME, FONT_SIZE, typographyElement } from './google-fonts.data'
7+
8+
/**
9+
* Follow guide https://medium.com/@lucgagan/mastering-playwright-best-practices-for-web-automation-with-the-page-object-model-3541412b03d1
10+
*/
11+
export class GoogleFontsPage extends CssBuilderPage {
12+
async addFontProps() {
13+
const fontLazyOption = this.page.locator(
14+
`.ant-select-item[title="${FONT_NAME}"]`,
15+
)
16+
17+
await this.page.locator('button span[aria-label="plus-square"]').click()
18+
await this.page.locator('div[name="fonts.0.type"]').click()
19+
await this.page.locator('.ant-select-item-option-active').hover()
20+
await this.scrollUntilElementIsVisible(fontLazyOption)
21+
await this.page.locator(`.ant-select-item[title="${FONT_NAME}"]`).click()
22+
await this.page.locator('div[name="value.weight"]').click()
23+
await this.page.locator(`.ant-select-item[title="${FONT_SIZE}"]`).click()
24+
await this.waitForProgressBar()
25+
}
26+
27+
async applyFonts() {
28+
await this.selectTreeElement(typographyElement)
29+
await this.openCssTab()
30+
31+
await this.page.locator('div[aria-label="Font"]').click()
32+
await this.page.locator('.ant-select-item[title="Montserrat"]').click()
33+
await this.waitForProgressBar()
34+
}
35+
36+
getOutputTextNode() {
37+
return this.getBuilderRenderContainer().locator('.ant-typography')
38+
}
39+
40+
async openPropsTab() {
41+
const cssTab = this.page.locator('[data-node-key="Props"]')
42+
43+
await cssTab.click()
44+
45+
await expect(cssTab).toHaveClass('ant-tabs-tab ant-tabs-tab-active')
46+
await expect(this.getSpinner()).toBeHidden()
47+
}
48+
49+
async scrollUntilElementIsVisible(locator: Locator) {
50+
while (!(await locator.isVisible())) {
51+
await this.page.mouse.wheel(0, 100)
52+
}
53+
}
54+
}
55+
56+
export const test = base.extend<{ builderPage: GoogleFontsPage }>({
57+
builderPage: async ({ page }, use) => {
58+
const builderPage = new GoogleFontsPage(page)
59+
60+
await use(builderPage)
61+
},
62+
})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { type IApp } from '@codelab/shared/abstract/core'
2+
import { providerPageId } from '@codelab/shared/data/test'
3+
import { expect } from '@playwright/test'
4+
5+
import {
6+
FONT_SIZE,
7+
googleFontsElement,
8+
seedTestData,
9+
} from './google-fonts.data'
10+
import { test } from './google-fonts.fixture'
11+
12+
let app: IApp
13+
14+
test.describe.configure({ mode: 'serial' })
15+
16+
test.beforeAll(async ({ request }, testInfo) => {
17+
app = await seedTestData(request)
18+
})
19+
20+
test.beforeEach(async ({ builderPage: page }) => {
21+
await page.goto(app.id, providerPageId)
22+
await page.checkPageHeaderTitle(['Codelab App', 'Pages', 'provider'])
23+
24+
await expect(page.getSpinner()).toBeHidden()
25+
await expect(page.getFormFieldSpinner()).toHaveCount(0)
26+
})
27+
28+
test('should create Google Fonts component and setup fonts on the _app page', async ({
29+
builderPage: page,
30+
}) => {
31+
const expectedLinkTag = page.page.locator('#render-root link')
32+
33+
await page.createElementTree([googleFontsElement])
34+
await page.selectTreeElement(googleFontsElement)
35+
await page.openPropsTab()
36+
await page.addFontProps()
37+
38+
await expect(expectedLinkTag).toHaveAttribute(
39+
'href',
40+
`https://fonts.googleapis.com/css2?family=Montserrat:wght@${FONT_SIZE}&display=swap`,
41+
)
42+
})
43+
44+
test('should apply selected fonts to text', async ({ builderPage: page }) => {
45+
await expect(page.getOutputTextNode()).toHaveText('Testing fonts')
46+
47+
const initialStyle = await page.getOutputTextNode().evaluate((element) => {
48+
return window.getComputedStyle(element)
49+
})
50+
51+
expect(initialStyle).not.toHaveProperty('fontFamily', 'Montserrat')
52+
expect(initialStyle).toHaveProperty('fontWeight', '500')
53+
54+
await page.applyFonts()
55+
56+
const updatedStyle = await page
57+
.getOutputTextNode()
58+
.evaluate((element) => window.getComputedStyle(element))
59+
60+
expect(updatedStyle).toHaveProperty('fontFamily', 'Montserrat')
61+
expect(updatedStyle).toHaveProperty('fontWeight', '700')
62+
})

0 commit comments

Comments
 (0)