Ghost/ghost/admin/tests/unit/helpers/most-recently-updated-test.js
Kevin Ansfield 17710c3e8e Switched members table to show info from most-recently-updated subscription instead of first in array
no issue

- members having multiple subscriptions is not really expected but if it does happen then the most recently updated subscription is most likely to be the one that we're interested in showing the details of in the members table
- added `{{most-recently-updated arr}}` helper+function that will return the item in the array argument with the most recent "updated at" value
  - uses `updatedAtUTC` or `updatedAt` or `updated_at` values so it will work against any of our models or un-transformed API response objects
- updated `<GhMembersListItemColumn>` to use a `mostRecentSubscription` getter when showing subscription data rather than assuming the first object in the subscriptions array should be displayed
2022-03-04 16:32:41 +00:00

65 lines
2.0 KiB
JavaScript

import moment from 'moment';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {mostRecentlyUpdated} from 'ghost-admin/helpers/most-recently-updated';
describe('Unit: Helper: most-recently-updated', function () {
it('returns most recent - updatedAtUTC', function () {
const a = {updatedAtUTC: moment.utc('2022-03-04 16:10')};
const b = {updatedAtUTC: moment.utc('2022-03-03 16:10')};
const c = {updatedAtUTC: moment.utc('2022-03-04 16:20')};
const subs = [a, b, c];
expect(mostRecentlyUpdated(subs)).to.equal(c);
});
it('returns most recent - updatedAt', function () {
const a = {updatedAt: moment('2022-03-04 16:10')};
const b = {updatedAt: moment('2022-03-05 16:10')};
const c = {updatedAt: moment('2022-03-04 16:20')};
const subs = [a, b, c];
expect(mostRecentlyUpdated(subs)).to.equal(b);
});
it('returns most recent - updated_at', function () {
const a = {updated_at: '2022-03-04 16:10'};
const b = {updated_at: '2022-03-03 16:10'};
const c = {updated_at: '2022-03-04 16:20'};
const subs = [a, b, c];
expect(mostRecentlyUpdated(subs)).to.equal(c);
});
it('handles a single-element array', function () {
const a = {updated_at: '2022-02-22'};
expect(mostRecentlyUpdated([a])).to.equal(a);
});
it('handles null', function () {
expect(mostRecentlyUpdated(null)).to.equal(null);
});
it('handles empty array', function () {
expect(mostRecentlyUpdated([])).to.equal(null);
});
it('does not modify original array', function () {
const a = {updated_at: '2022-03-04 16:10'};
const b = {updated_at: '2022-03-03 16:10'};
const c = {updated_at: '2022-03-04 16:20'};
const subs = [a, b, c];
mostRecentlyUpdated(subs);
expect(subs[0]).to.equal(a);
expect(subs[1]).to.equal(b);
expect(subs[2]).to.equal(c);
});
});