Skip to content

Commit

Permalink
chore: separate Script Loader module
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Aug 21, 2024
1 parent 9cfad15 commit 99da235
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 69 deletions.
69 changes: 0 additions & 69 deletions tests/e2e/specs/admin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
*/
import { test, expect } from '@wordpress/e2e-test-utils-playwright';

/**
* Internal dependencies
*/
import { blockRequests } from '../utils';

test.describe( 'Admin', () => {

test( 'check About Us page', async({ page }) => {
Expand All @@ -32,68 +27,4 @@ test.describe( 'Admin', () => {
// Landing Kit link is visible.
await expect( page.getByRole( 'link', { name: 'Learn More' }) ).toBeVisible();
});

test( 'check survey and tracking script loading (on Visualizer)', async({ page, admin, context }) => {
await blockRequests( context );
await admin.visitAdminPage( 'admin.php?page=visualizer' );

await expect( page.locator( 'script#themeisle_sdk_survey_script-js' ) ).toHaveCount( 1 );
await expect( page.locator( 'script#themeisle_sdk_tracking_script-js' ) ).toHaveCount( 1 );
await expect( page.locator( 'script#themeisle_sdk_tracking_script-js-extra' ) ).toHaveCount( 1 );

const tiTelemetryDefined = await page.evaluate( () => {
return 'undefined' !== typeof window.tiTelemetry;
});
expect( tiTelemetryDefined ).toBe( true );
});

test( 'check window.tiTrk functionality', async({ page, admin, context }) => {
await admin.visitAdminPage( 'admin.php?page=visualizer' );
await blockRequests( context );

// Check if window.tiTrk exists
const tiTrkExists = await page.evaluate( () => {
return 'undefined' !== typeof window.tiTrk;
});
expect( tiTrkExists ).toBe( true );

// Check if window.tiTrk has the expected methods
const tiTrkMethods = await page.evaluate( () => {
return {
hasWithMethod: 'function' === typeof window.tiTrk.with,
hasUploadEventsMethod: 'function' === typeof window.tiTrk.uploadEvents,
hasStartMethod: 'function' === typeof window.tiTrk.start,
hasStopMethod: 'function' === typeof window.tiTrk.stop
};
});
expect( tiTrkMethods ).toEqual({
hasWithMethod: true,
hasUploadEventsMethod: true,
hasStartMethod: true,
hasStopMethod: true
});

// Test adding an event
const eventAdded = await page.evaluate( () => {
const testEvent = {
slug: 'visualizer',
action: 'test-action',
feature: 'test-feature'
};
window.tiTrk.with( 'visualizer' ).add( testEvent );
return 0 < window.tiTrk.events.size;
});
expect( eventAdded ).toBe( true );

// Test uploading events (this will clear the events)
await page.evaluate( () => {
window.tiTrk.uploadEvents();
});

// Check if events were cleared after uploading
const eventsCleared = await page.evaluate( () => {
return 0 === window.tiTrk.events.size;
});
expect( eventsCleared ).toBe( true );
});
});
76 changes: 76 additions & 0 deletions tests/e2e/specs/script-loader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* WordPress dependencies
*/
import { test, expect } from '@wordpress/e2e-test-utils-playwright';

/**
* Internal dependencies
*/
import { blockRequests } from '../utils';

test.describe( 'Admin', () => {

test( 'check survey and tracking script loading (on Visualizer)', async({ page, admin, context }) => {
await blockRequests( context );
await admin.visitAdminPage( 'admin.php?page=visualizer' );

await expect( page.locator( 'script#themeisle_sdk_survey_script-js' ) ).toHaveCount( 1 );
await expect( page.locator( 'script#themeisle_sdk_tracking_script-js' ) ).toHaveCount( 1 );
await expect( page.locator( 'script#themeisle_sdk_tracking_script-js-extra' ) ).toHaveCount( 1 );

const tiTelemetryDefined = await page.evaluate( () => {
return 'undefined' !== typeof window.tiTelemetry;
});
expect( tiTelemetryDefined ).toBe( true );
});

test( 'check window.tiTrk functionality', async({ page, admin, context }) => {
await admin.visitAdminPage( 'admin.php?page=visualizer' );
await blockRequests( context );

// Check if window.tiTrk exists
const tiTrkExists = await page.evaluate( () => {
return 'undefined' !== typeof window.tiTrk;
});
expect( tiTrkExists ).toBe( true );

// Check if window.tiTrk has the expected methods
const tiTrkMethods = await page.evaluate( () => {
return {
hasWithMethod: 'function' === typeof window.tiTrk.with,
hasUploadEventsMethod: 'function' === typeof window.tiTrk.uploadEvents,
hasStartMethod: 'function' === typeof window.tiTrk.start,
hasStopMethod: 'function' === typeof window.tiTrk.stop
};
});
expect( tiTrkMethods ).toEqual({
hasWithMethod: true,
hasUploadEventsMethod: true,
hasStartMethod: true,
hasStopMethod: true
});

// Test adding an event
const eventAdded = await page.evaluate( () => {
const testEvent = {
slug: 'visualizer',
action: 'test-action',
feature: 'test-feature'
};
window.tiTrk.with( 'visualizer' ).add( testEvent );
return 0 < window.tiTrk.events.size;
});
expect( eventAdded ).toBe( true );

// Test uploading events (this will clear the events)
await page.evaluate( () => {
window.tiTrk.uploadEvents();
});

// Check if events were cleared after uploading
const eventsCleared = await page.evaluate( () => {
return 0 === window.tiTrk.events.size;
});
expect( eventsCleared ).toBe( true );
});
});

0 comments on commit 99da235

Please sign in to comment.