-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Listing the utility function in separate file.
- Loading branch information
Showing
4 changed files
with
109 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
assets/js/modules/analytics-4/utils/report-rows-with-set-values.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Analytics 4 filter rows having (not set) values. | ||
* | ||
* Site Kit by Google, Copyright 2024 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. | ||
*/ | ||
|
||
export function reportRowsWithSetValues( rows, maxRows = 3 ) { | ||
const rowsWithSetValues = rows.filter( | ||
( { dimensionValues } ) => dimensionValues[ 0 ].value !== '(not set)' | ||
); | ||
|
||
return rowsWithSetValues.slice( 0, maxRows ); | ||
} |
80 changes: 80 additions & 0 deletions
80
assets/js/modules/analytics-4/utils/report-rows-with-set-values.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Analytics-4 filter rows having (not set) values tests. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import { reportRowsWithSetValues } from './report-rows-with-set-values'; | ||
|
||
describe( 'reportRowsWithSetValues', () => { | ||
const mockRows = [ | ||
{ dimensionValues: [ { value: 'value1' } ] }, | ||
{ dimensionValues: [ { value: '(not set)' } ] }, | ||
{ dimensionValues: [ { value: 'value2' } ] }, | ||
{ dimensionValues: [ { value: 'value3' } ] }, | ||
{ dimensionValues: [ { value: 'value4' } ] }, | ||
]; | ||
|
||
it( 'should return rows with set dimension values only', () => { | ||
const result = reportRowsWithSetValues( mockRows ); | ||
expect( result ).toEqual( [ | ||
{ dimensionValues: [ { value: 'value1' } ] }, | ||
{ dimensionValues: [ { value: 'value2' } ] }, | ||
{ dimensionValues: [ { value: 'value3' } ] }, | ||
] ); | ||
} ); | ||
|
||
it( 'should limit the number of returned rows to maxRows', () => { | ||
const result = reportRowsWithSetValues( mockRows, 2 ); | ||
expect( result ).toEqual( [ | ||
{ dimensionValues: [ { value: 'value1' } ] }, | ||
{ dimensionValues: [ { value: 'value2' } ] }, | ||
] ); | ||
} ); | ||
|
||
it( 'should handle maxRows greater than the number of filtered rows', () => { | ||
const result = reportRowsWithSetValues( mockRows, 10 ); | ||
expect( result ).toEqual( [ | ||
{ dimensionValues: [ { value: 'value1' } ] }, | ||
{ dimensionValues: [ { value: 'value2' } ] }, | ||
{ dimensionValues: [ { value: 'value3' } ] }, | ||
{ dimensionValues: [ { value: 'value4' } ] }, | ||
] ); | ||
} ); | ||
|
||
it( 'should handle empty rows input', () => { | ||
const result = reportRowsWithSetValues( [] ); | ||
expect( result ).toEqual( [] ); | ||
} ); | ||
|
||
it( 'should handle rows where all values are "(not set)"', () => { | ||
const allNotSetRows = [ | ||
{ dimensionValues: [ { value: '(not set)' } ] }, | ||
{ dimensionValues: [ { value: '(not set)' } ] }, | ||
]; | ||
const result = reportRowsWithSetValues( allNotSetRows ); | ||
expect( result ).toEqual( [] ); | ||
} ); | ||
|
||
it( 'should return an empty array when maxRows is 0', () => { | ||
const result = reportRowsWithSetValues( mockRows, 0 ); | ||
expect( result ).toEqual( [] ); | ||
} ); | ||
|
||
it( 'should use the default maxRows value of 3 when not specified', () => { | ||
const result = reportRowsWithSetValues( mockRows ); | ||
expect( result ).toHaveLength( 3 ); | ||
} ); | ||
} ); |