Skip to content

Commit

Permalink
Add tests for TopConvertingTrafficSourceWidget.
Browse files Browse the repository at this point in the history
  • Loading branch information
techanvil committed Jul 4, 2023
1 parent af7f070 commit ced0833
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ export default function TopConvertingTrafficSourceWidget( {
orderBy: 'sessionConversionRate',
};

const report = useInViewSelect( ( select ) =>
select( MODULES_ANALYTICS_4 ).getReport( reportOptions )
);
const report = useInViewSelect( ( select ) => {
if ( keyMetricsWidgetHidden !== false ) {
return null;
}

return select( MODULES_ANALYTICS_4 ).getReport( reportOptions );
} );

const loading = useSelect(
( select ) =>
Expand All @@ -95,7 +99,7 @@ export default function TopConvertingTrafficSourceWidget( {
dateValue.value === dateRange
);

// As the report is limited to 1 row, return the first row.
// As the report is limited to 1 row per date range, return the first row.
return rows[ 0 ];
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* TopConvertingTrafficSourceWidget component tests.
*
* Site Kit by Google, Copyright 2023 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.
*/

/**
* Internal dependencies
*/
import { render } from '../../../../../../tests/js/test-utils';
import { provideKeyMetrics } from '../../../../../../tests/js/utils';
import { provideAnalytics4MockReport } from '../../utils/data-mock';
import { getWidgetComponentProps } from '../../../../googlesitekit/widgets/util';
import {
CORE_USER,
KM_ANALYTICS_TOP_CONVERTING_TRAFFIC_SOURCE,
} from '../../../../googlesitekit/datastore/user/constants';
import TopConvertingTrafficSourceWidget from './TopConvertingTrafficSourceWidget';

describe( 'TopConvertingTrafficSourceWidget', () => {
const { Widget, WidgetNull } = getWidgetComponentProps(
KM_ANALYTICS_TOP_CONVERTING_TRAFFIC_SOURCE
);

it.each( [ undefined, true ] )(
'renders null when isKeyMetricsWidgetHidden() returns %s',
async ( isWidgetHidden ) => {
const { container, waitForRegistry } = render(
<TopConvertingTrafficSourceWidget
Widget={ Widget }
WidgetNull={ WidgetNull }
/>,
{
setupRegistry: ( registry ) => {
provideKeyMetrics( registry, { isWidgetHidden } );
},
}
);
await waitForRegistry();

expect( container ).toBeEmptyDOMElement();
}
);

it( 'renders correctly with the expected metrics when the Key Metrics widget is not hidden', async () => {
const { container, waitForRegistry } = render(
<TopConvertingTrafficSourceWidget
Widget={ Widget }
WidgetNull={ WidgetNull }
/>,
{
setupRegistry: ( registry ) => {
registry
.dispatch( CORE_USER )
.setReferenceDate( '2020-09-08' );

provideKeyMetrics( registry );
provideAnalytics4MockReport( registry, {
compareStartDate: '2020-07-14',
compareEndDate: '2020-08-10',
startDate: '2020-08-11',
endDate: '2020-09-07',
dimensions: [ 'sessionDefaultChannelGroup' ],
metrics: [
{
name: 'sessionConversionRate',
},
],
limit: 1,
orderBy: 'sessionConversionRate',
} );
},
}
);
await waitForRegistry();

expect( container ).toMatchSnapshot();
} );
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TopConvertingTrafficSourceWidget renders correctly with the expected metrics when the Key Metrics widget is not hidden 1`] = `
<div>
<div
class="googlesitekit-widget googlesitekit-widget--kmAnalyticsTopConvertingTrafficSource googlesitekit-widget--no-padding"
>
<div
class="googlesitekit-widget__body"
>
<div
class="googlesitekit-km-widget-tile googlesitekit-km-widget-tile--text"
>
<h3
class="googlesitekit-km-widget-tile__title"
>
Top converting traffic source
</h3>
<div
class="googlesitekit-km-widget-tile__body"
>
<div
class="googlesitekit-km-widget-tile__metric"
>
Organic Search
</div>
<p
class="googlesitekit-km-widget-tile__subtext"
>
54.9% of visits led to conversions
</p>
<div
class="googlesitekit-km-widget-tile__metric-change-container"
>
<div
class="googlesitekit-change-badge"
>
+40.7%
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;

0 comments on commit ced0833

Please sign in to comment.