Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean up for PR and forgotten karma test for errors.
Browse files Browse the repository at this point in the history
BrittanyIRL committed Aug 10, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d4ce2bc commit 9097fc3
Showing 5 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ export const TEXT = {
SECTION_HEADING: __('Google Analytics Tracking ID', 'web-stories'),
PLACEHOLDER: __('Enter your Google Analtyics Tracking ID', 'web-stories'),
ARIA_LABEL: __('Enter your Google Analtyics Tracking ID', 'web-stories'),
INPUT_ERROR: __('Invalid ID format', 'web-stories'),
};

function GoogleAnalyticsSettings({
@@ -61,12 +62,11 @@ function GoogleAnalyticsSettings({

const handleUpdateId = useCallback(
(value) => {
// todo add validation to string format
if (value.length === 0 || validateGoogleAnalyticsIdFormat(value)) {
setInputError('');
return handleUpdateSettings({ newGoogleAnalyticsId: value });
}
return setInputError('Invalid ID format');
return setInputError(TEXT.INPUT_ERROR);
},
[handleUpdateSettings, setInputError]
);
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ export const _default = () => {
return (
<GoogleAnalyticsSettings
onUpdateGoogleAnalyticsId={action('update google analytics id submitted')}
googleAnalyticsId={text('googleAnalyticsId', '638261718182736363-83737')}
googleAnalyticsId={text('googleAnalyticsId', 'UA-000000-98')}
canManageSettings={true}
/>
);
@@ -44,7 +44,7 @@ export const _CannotManageSettings = () => {
return (
<GoogleAnalyticsSettings
onUpdateGoogleAnalyticsId={action('update google analytics id submitted')}
googleAnalyticsId={text('googleAnalyticsId', '638261718182736363-83737')}
googleAnalyticsId={text('googleAnalyticsId', 'UA-000000-98')}
/>
);
};
Original file line number Diff line number Diff line change
@@ -91,5 +91,27 @@ describe('Settings View', () => {
expect(newInput.value).toBe(googleAnalyticsId);
});

// it("it should not allow an update of google analytics id when id format doesn't match required format", () => {});
it("it should not allow an update of google analytics id when id format doesn't match required format", async () => {
const settingsView = await fixture.screen.getByTestId('editor-settings');

const input = within(settingsView).getByRole('textbox');

await fixture.events.hover(input);

await fixture.events.click(input);

const inputLength = input.value.length;

for (let iter = 0; iter < inputLength; iter++) {
// disable eslint to prevent overlapping .act calls
// eslint-disable-next-line no-await-in-loop
await fixture.events.keyboard.press('Backspace');
}

await fixture.events.keyboard.type('Clearly not a valid id');
await fixture.events.keyboard.press('Enter');

const errorMessage = await fixture.screen.getByText('Invalid ID format');
expect(errorMessage).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -22,9 +22,10 @@ import PropTypes from 'prop-types';
* Internal dependencies
*/
import { renderWithTheme } from '../../../../testUtils';
import EditorSettings from '../';
import { ApiContext } from '../../../api/apiProvider';
import { ConfigProvider } from '../../../config';
import EditorSettings from '../';
import { TEXT as GA_TEXT } from '../googleAnalytics';

const mockFetchSettings = jest.fn();

@@ -63,12 +64,9 @@ describe('Editor Settings: <Editor Settings />', function () {
<SettingsWrapper googleAnalyticsId="123-45-98-not-an-id" />
);

const googleAnalyticsHeading = getByText('Google Analytics Tracking ID');
const googleAnalyticsHeading = getByText(GA_TEXT.SECTION_HEADING);
expect(googleAnalyticsHeading).toBeInTheDocument();

const publisherLogoHeading = getByText('Publisher Logo');
expect(publisherLogoHeading).toBeInTheDocument();

const input = getByRole('textbox');
expect(input).toBeDefined();

17 changes: 5 additions & 12 deletions assets/src/dashboard/utils/test/validateGoogleAnalyticsIdFormat.js
Original file line number Diff line number Diff line change
@@ -19,26 +19,19 @@
*/
import { validateGoogleAnalyticsIdFormat } from '../';

const validIdFormats = [
const idsToValidate = [
['UA-000000-56', true],
['ua-098765432-9875', true],
];
const invalidIdFormats = [
['78787878', false],
['ua--123448-0', false],
['clearly wrong', false],
];

describe('validateGoogleAnalyticsIdFormat', () => {
it.each(validIdFormats)('should return %s as true', (validId, expected) => {
const bool = validateGoogleAnalyticsIdFormat(validId);
expect(bool).toBe(expected);
});

it.each(invalidIdFormats)(
'should return %s as false',
(invalidId, expected) => {
const bool = validateGoogleAnalyticsIdFormat(invalidId);
it.each(idsToValidate)(
'should check if %s is valid %p %p',
(validId, expected) => {
const bool = validateGoogleAnalyticsIdFormat(validId);
expect(bool).toBe(expected);
}
);

0 comments on commit 9097fc3

Please sign in to comment.