[web] Migrate managed publishers to React dash (#11749)

* Migrate managed publishers to React dash

* Decaf cleanup + async/await PublishersGetter

* Continue migration of managed publishers to react dash

* Fix linting

* Add tests

* Decaf cleanup PublishersGetterTests

* Update PublishersGetter tests

* Rename component files to kebab-case

GitOrigin-RevId: cb1fe14d120457c965a9d23a8ddb2c2c92e1d5da
This commit is contained in:
Alexandre Bourdin
2023-02-23 09:04:03 +00:00
committed by Copybot
parent b30d838c49
commit d6e9508aed
13 changed files with 232 additions and 66 deletions
@@ -0,0 +1,85 @@
import { expect } from 'chai'
import { render, screen } from '@testing-library/react'
import { SubscriptionDashboardProvider } from '../../../../../../frontend/js/features/subscription/context/subscription-dashboard-context'
import fetchMock from 'fetch-mock'
import ManagedPublishers, {
Publisher,
} from '../../../../../../frontend/js/features/subscription/components/dashboard/managed-publishers'
const userId = 'fff999fff999'
const publisher1 = {
slug: 'pub-1',
managerIds: [],
name: 'Pub 1',
partner: 'p1',
}
const publisher2 = {
slug: 'pub-2',
managerIds: [],
name: 'Pub 2',
partner: 'p2',
}
const managedPublishers: Publisher[] = [publisher1, publisher2]
describe('<ManagedPublishers />', function () {
beforeEach(function () {
window.metaAttributesCache = new Map()
window.metaAttributesCache.set('ol-managedPublishers', managedPublishers)
window.user_id = userId
})
afterEach(function () {
window.metaAttributesCache = new Map()
delete window.user_id
fetchMock.reset()
})
it('renders all managed publishers', function () {
render(
<SubscriptionDashboardProvider>
<ManagedPublishers />
</SubscriptionDashboardProvider>
)
const elements = screen.getAllByText('You are a', {
exact: false,
})
expect(elements.length).to.equal(2)
expect(elements[0].textContent).to.equal('You are a manager of Pub 1')
expect(elements[1].textContent).to.equal('You are a manager of Pub 2')
const viewHubLinks = screen.getAllByText('View hub')
expect(viewHubLinks.length).to.equal(2)
expect(viewHubLinks[0].getAttribute('href')).to.equal(
'/publishers/pub-1/hub'
)
expect(viewHubLinks[1].getAttribute('href')).to.equal(
'/publishers/pub-2/hub'
)
const manageGroupManagersLinks = screen.getAllByText(
'Manage publisher managers'
)
expect(manageGroupManagersLinks.length).to.equal(2)
expect(manageGroupManagersLinks[0].getAttribute('href')).to.equal(
'/manage/publishers/pub-1/managers'
)
expect(manageGroupManagersLinks[1].getAttribute('href')).to.equal(
'/manage/publishers/pub-2/managers'
)
})
it('renders nothing when there are no publishers', function () {
window.metaAttributesCache.set('ol-managedPublishers', undefined)
render(
<SubscriptionDashboardProvider>
<ManagedPublishers />
</SubscriptionDashboardProvider>
)
const elements = screen.queryAllByText('You are a', {
exact: false,
})
expect(elements.length).to.equal(0)
})
})
@@ -1,19 +1,6 @@
/* eslint-disable
n/handle-callback-err,
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const { expect } = require('chai')
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = require('path').join(
__dirname,
'../../../../app/src/Features/Publishers/PublishersGetter.js'
@@ -27,38 +14,41 @@ describe('PublishersGetter', function () {
fetchV1Data: sinon.stub(),
}
this.UserMembershipsHandler = {
promises: {
getEntitiesByUser: sinon.stub().resolves([this.publisher]),
},
}
this.UserMembershipEntityConfigs = {
publisher: {
modelName: 'Publisher',
canCreate: true,
fields: {
primaryKey: 'slug',
},
},
}
this.PublishersGetter = SandboxedModule.require(modulePath, {
requires: {
'../User/UserGetter': this.UserGetter,
'../UserMembership/UserMembershipsHandler':
(this.UserMembershipsHandler = {
getEntitiesByUser: sinon
.stub()
.callsArgWith(2, null, [this.publisher]),
}),
'../UserMembership/UserMembershipsHandler': this.UserMembershipsHandler,
'../UserMembership/UserMembershipEntityConfigs':
(this.UserMembershipEntityConfigs = {
publisher: {
modelName: 'Publisher',
canCreate: true,
fields: {
primaryKey: 'slug',
},
},
}),
this.UserMembershipEntityConfigs,
},
})
return (this.userId = '12345abcde')
this.userId = '12345abcde'
})
describe('getManagedPublishers', function () {
it('fetches v1 data before returning publisher list', function (done) {
return this.PublishersGetter.getManagedPublishers(
this.PublishersGetter.getManagedPublishers(
this.userId,
(error, publishers) => {
expect(error).to.be.null
publishers.length.should.equal(1)
return done()
done()
}
)
})