Skip to content

Commit

Permalink
Merge branch 'main' into develop.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Apr 11, 2022
2 parents 7bb38f9 + a0adeaf commit d3aed2e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 4 deletions.
15 changes: 11 additions & 4 deletions assets/js/googlesitekit/datastore/user/surveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,21 @@ const baseActions = {
},
function* ( triggerID, options = {} ) {
const { ttl = 0 } = options;
const { select } = yield Data.commonActions.getRegistry();
const {
select,
__experimentalResolveSelect,
} = yield Data.commonActions.getRegistry();

if ( ! select( CORE_USER ).isAuthenticated() ) {
// Bail if there is already a current survey.
if ( select( CORE_USER ).getCurrentSurvey() ) {
return {};
}
// Wait for user authentication state to be available before selecting.
yield Data.commonActions.await(
__experimentalResolveSelect( CORE_USER ).getAuthentication()
);

// Bail if there is already a current survey.
if ( select( CORE_USER ).getCurrentSurvey() ) {
if ( ! select( CORE_USER ).isAuthenticated() ) {
return {};
}

Expand Down
22 changes: 22 additions & 0 deletions assets/js/googlesitekit/datastore/user/surveys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ describe( 'core/user surveys', () => {
} );
} );

it( 'waits for authentication to be resolved before making a network request', async () => {
muteFetch( surveyTriggerEndpoint );
fetchMock.getOnce(
/^\/google-site-kit\/v1\/core\/user\/data\/authentication/,
{
authenticated: true,
}
);
const triggerSurveyPromise = registry
.dispatch( CORE_USER )
.triggerSurvey( 'optimizeSurvey' );

expect( fetchMock ).not.toHaveFetched( surveyTriggerEndpoint );

await triggerSurveyPromise;
expect( fetchMock ).toHaveFetched( surveyTriggerEndpoint, {
body: {
data: { triggerID: 'optimizeSurvey' },
},
} );
} );

it( 'makes network requests to survey endpoint', async () => {
provideUserAuthentication( registry );
muteFetch( surveyTriggerEndpoint );
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Please create a new topic on our [WordPress.org support forum](https://wordpress

**Fixed**

* Fix bug where user surveys would not trigger when viewing the dashboard. See [#5073](https://github.com/google/site-kit-wp/issues/5073).
* When Analytics is gathering data, ensure "gathering data" is shown under All Users in the All Traffic widget. See [#5007](https://github.com/google/site-kit-wp/issues/5007).
* Ensure WordPress dashboard notices appear in Site Kit. See [#4998](https://github.com/google/site-kit-wp/issues/4998).
* Don't show the "gathering data" blue box CTA on the Admin Bar. See [#4986](https://github.com/google/site-kit-wp/issues/4986).
Expand Down
87 changes: 87 additions & 0 deletions tests/e2e/specs/dashboard/surveys.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Dashboard surveys test.
*
* Site Kit by Google, Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* WordPress dependencies
*/
import { visitAdminPage } from '@wordpress/e2e-test-utils';

/**
* Internal dependencies
*/
import {
enableFeature,
setupSiteKit,
useRequestInterception,
} from '../../utils';
import surveyResponse from '../../../../assets/js/components/surveys/__fixtures__/survey-single-question.json';

describe( 'dashboard surveys', () => {
beforeAll( async () => {
await setupSiteKit();

await page.setRequestInterception( true );
useRequestInterception( ( request ) => {
const url = request.url();

// The survey endpoint is stubbed on the server to always return a null survey
// so we will intercept it on the client (see e2e-rest-survey-trigger.php).
if (
url.match( '/google-site-kit/v1/core/user/data/survey-trigger' )
) {
request.respond( {
status: 200,
body: JSON.stringify( surveyResponse ),
} );
} else if (
url.match( '/google-site-kit/v1/core/user/data/survey-event' )
) {
request.respond( { status: 200 } );
} else if (
url.match(
'/google-site-kit/v1/modules/search-console/data/searchanalytics'
)
) {
request.respond( {
status: 200,
body: JSON.stringify( [] ),
} );
} else {
request.continue();
}
} );
} );

it( 'shows a survey', async () => {
await visitAdminPage( 'admin.php', 'page=googlesitekit-dashboard' );

await expect( page ).toMatchElement( '.googlesitekit-survey', {
text: surveyResponse.survey_payload.question[ 0 ].question_text,
} );
} );

it( 'shows a survey on the unified dashboard', async () => {
await enableFeature( 'unifiedDashboard' );

await visitAdminPage( 'admin.php', 'page=googlesitekit-dashboard' );

await expect( page ).toMatchElement( '.googlesitekit-survey', {
text: surveyResponse.survey_payload.question[ 0 ].question_text,
} );
} );
} );

0 comments on commit d3aed2e

Please sign in to comment.