Skip to content

Commit ec27824

Browse files
committed
Stats: align statsVideo normalizer with #112499 to avoid merge conflicts
Adopts the identical normalizer hunk and tests from the video details modernization branch (Array.isArray guard on payload.data and the attachment post passthrough) so whichever PR lands second merges cleanly.
1 parent 2d336b6 commit ec27824

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

client/state/stats/lists/test/utils.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,30 @@ describe( 'utils', () => {
16361636
expect( normalizers.statsVideo() ).toBeNull();
16371637
} );
16381638

1639+
test( 'should return empty data when the endpoint reports an empty window', () => {
1640+
// With no rows in the requested window, the endpoint returns a
1641+
// single object instead of the usual [ date, value ] tuples.
1642+
expect(
1643+
normalizers.statsVideo( {
1644+
data: { date: '7-10', p: '0' },
1645+
pages: [],
1646+
} )
1647+
).toEqual( { pages: [], data: [], post: null } );
1648+
} );
1649+
1650+
test( 'should skip non-tuple entries in the data array', () => {
1651+
expect(
1652+
normalizers.statsVideo( {
1653+
data: [ [ '2016-11-12', 1 ], { date: '7-10', p: '0' } ],
1654+
pages: [],
1655+
} )
1656+
).toEqual( {
1657+
pages: [],
1658+
data: [ { period: '2016-11-12', value: 1 } ],
1659+
post: null,
1660+
} );
1661+
} );
1662+
16391663
test( 'should return a properly parsed data array', () => {
16401664
expect(
16411665
normalizers.statsVideo( {
@@ -1675,23 +1699,20 @@ describe( 'utils', () => {
16751699
link: 'http://www.themepremium.com/blog-with-the-speed-of-your-thought-with-the-p2-theme/',
16761700
},
16771701
],
1702+
post: null,
16781703
} );
16791704
} );
16801705

1681-
test( 'should drop the stub object row returned for videos with no plays', () => {
1682-
expect(
1683-
normalizers.statsVideo( {
1684-
data: [ { date: '7-10', p: '0' } ],
1685-
pages: [ 'https://vip.wordpress.com/category/themes/' ],
1686-
} )
1687-
).toEqual( {
1706+
test( 'should pass through the attachment post', () => {
1707+
const post = {
1708+
ID: 43948,
1709+
post_title: 'blank-canvas-split-screen',
1710+
post_date: '2021-02-08 13:53:37',
1711+
};
1712+
expect( normalizers.statsVideo( { data: [], pages: [], post } ) ).toEqual( {
1713+
pages: [],
16881714
data: [],
1689-
pages: [
1690-
{
1691-
label: 'https://vip.wordpress.com/category/themes/',
1692-
link: 'https://vip.wordpress.com/category/themes/',
1693-
},
1694-
],
1715+
post,
16951716
} );
16961717
} );
16971718
} );

client/state/stats/lists/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,9 +834,9 @@ export const normalizers = {
834834
}
835835

836836
let data = [];
837-
if ( payload.data ) {
838-
// When a video has no plays for the period, the API returns a stub object
839-
// (e.g. `{ date: '7-10', p: '0' }`) instead of a `[ period, value ]` pair.
837+
// When the requested window has no rows at all, the endpoint returns a single
838+
// `{ date, p }` object instead of the usual `[ date, value ]` tuples.
839+
if ( Array.isArray( payload.data ) ) {
840840
data = payload.data
841841
.filter( ( item ) => Array.isArray( item ) )
842842
.map( ( item ) => {
@@ -854,7 +854,9 @@ export const normalizers = {
854854
} );
855855
}
856856

857-
return { pages, data };
857+
// The endpoint also returns the video's attachment post, which carries
858+
// the title and upload date.
859+
return { pages, data, post: payload.post ?? null };
858860
},
859861

860862
/**

0 commit comments

Comments
 (0)