12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205 |
- import {mountWithTheme} from 'sentry-test/enzyme';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {mockRouterPush} from 'sentry-test/mockRouterPush';
- import * as globalActions from 'app/actionCreators/globalSelection';
- import OrganizationActions from 'app/actions/organizationActions';
- import GlobalSelectionHeader from 'app/components/organizations/globalSelectionHeader';
- import ConfigStore from 'app/stores/configStore';
- import GlobalSelectionStore from 'app/stores/globalSelectionStore';
- import ProjectsStore from 'app/stores/projectsStore';
- import {getItem} from 'app/utils/localStorage';
- const changeQuery = (routerContext, query) => ({
- ...routerContext,
- context: {
- ...routerContext.context,
- router: {
- ...routerContext.context.router,
- location: {
- query,
- },
- },
- },
- });
- jest.mock('app/utils/localStorage', () => ({
- getItem: jest.fn(),
- setItem: jest.fn(),
- }));
- describe('GlobalSelectionHeader', function () {
- let wrapper;
- const {organization, router, routerContext} = initializeOrg({
- organization: {features: ['global-views']},
- projects: [
- {
- id: 2,
- slug: 'project-2',
- },
- {
- id: 3,
- slug: 'project-3',
- environments: ['prod', 'staging'],
- },
- ],
- router: {
- location: {query: {}},
- params: {orgId: 'org-slug'},
- },
- });
- beforeAll(function () {
- jest.spyOn(globalActions, 'updateDateTime');
- jest.spyOn(globalActions, 'updateEnvironments');
- jest.spyOn(globalActions, 'updateProjects');
- jest.spyOn(globalActions, 'updateParams');
- jest.spyOn(globalActions, 'updateParamsWithoutHistory');
- });
- beforeEach(function () {
- MockApiClient.clearMockResponses();
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => organization.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- getItem.mockImplementation(() => null);
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/projects/',
- body: [],
- });
- });
- afterEach(function () {
- wrapper.unmount();
- [
- globalActions.updateDateTime,
- globalActions.updateProjects,
- globalActions.updateEnvironments,
- globalActions.updateParams,
- globalActions.updateParamsWithoutHistory,
- router.push,
- router.replace,
- getItem,
- ].forEach(mock => mock.mockClear());
- GlobalSelectionStore.reset();
- });
- it('does not update router if there is custom routing', function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={organization} hasCustomRouting />,
- routerContext
- );
- expect(router.push).not.toHaveBeenCalled();
- });
- it('does not update router if org in URL params is different than org in context/props', function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={organization} hasCustomRouting />,
- {
- ...routerContext,
- context: {
- ...routerContext.context,
- router: {...routerContext.context.router, params: {orgId: 'diff-org'}},
- },
- }
- );
- expect(router.push).not.toHaveBeenCalled();
- });
- it('does not replace URL with values from store when mounted with no query params', function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={organization} />,
- routerContext
- );
- expect(router.replace).not.toHaveBeenCalled();
- });
- it('only updates GlobalSelection store when mounted with query params', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={organization}
- params={{orgId: organization.slug}}
- />,
- changeQuery(routerContext, {
- statsPeriod: '7d',
- })
- );
- expect(router.push).not.toHaveBeenCalled();
- await tick();
- expect(GlobalSelectionStore.get().selection).toEqual({
- datetime: {
- period: '7d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- });
- });
- it('updates environments when switching projects', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={organization}
- projects={organization.projects}
- />,
- routerContext
- );
- await tick();
- wrapper.update();
- mockRouterPush(wrapper, router);
- // Open dropdown and select both projects
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- wrapper.find('MultipleProjectSelector CheckboxFancy').at(0).simulate('click');
- wrapper.find('MultipleProjectSelector CheckboxFancy').at(1).simulate('click');
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- await tick();
- wrapper.update();
- expect(wrapper.find('MultipleProjectSelector Content').text()).toBe(
- 'project-2, project-3'
- );
- // Select environment
- wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
- wrapper.find('MultipleEnvironmentSelector CheckboxFancy').at(1).simulate('click');
- wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
- await tick();
- expect(wrapper.find('MultipleEnvironmentSelector Content').text()).toBe('staging');
- expect(GlobalSelectionStore.get().selection).toEqual({
- datetime: {
- period: '14d',
- utc: null,
- start: null,
- end: null,
- },
- environments: ['staging'],
- projects: [2, 3],
- });
- const query = wrapper.prop('location').query;
- expect(query).toEqual({
- environment: 'staging',
- project: ['2', '3'],
- });
- // Now change projects, first project has no environments
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- wrapper.find('MultipleProjectSelector CheckboxFancy').at(1).simulate('click');
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- await tick();
- wrapper.update();
- // Store should not have any environments selected
- expect(GlobalSelectionStore.get().selection).toEqual({
- datetime: {
- period: '14d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [2],
- });
- expect(wrapper.find('MultipleEnvironmentSelector Content').text()).toBe(
- 'All Environments'
- );
- });
- it('shows environments for non-member projects', async function () {
- const initialData = initializeOrg({
- organization: {features: ['global-views']},
- projects: [
- {id: 1, slug: 'staging-project', environments: ['staging'], isMember: false},
- {id: 2, slug: 'prod-project', environments: ['prod']},
- ],
- router: {
- location: {query: {project: [1]}},
- params: {orgId: 'org-slug'},
- },
- });
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- router={initialData.router}
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- changeQuery(initialData.routerContext, {project: 1})
- );
- await tick();
- wrapper.update();
- // Open environment picker
- wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
- const checkboxes = wrapper.find('MultipleEnvironmentSelector AutoCompleteItem');
- expect(checkboxes).toHaveLength(1);
- expect(checkboxes.text()).toBe('staging');
- });
- it('updates GlobalSelection store with default period', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={organization} />,
- changeQuery(routerContext, {
- environment: 'prod',
- })
- );
- await tick();
- expect(GlobalSelectionStore.get()).toEqual({
- isReady: true,
- selection: {
- datetime: {
- period: '14d',
- utc: null,
- start: null,
- end: null,
- },
- environments: ['prod'],
- projects: [],
- },
- });
- // Not called because of the default date
- expect(router.replace).not.toHaveBeenCalled();
- });
- it('updates GlobalSelection store with empty dates in URL', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={organization} />,
- changeQuery(routerContext, {
- statsPeriod: null,
- })
- );
- await tick();
- expect(GlobalSelectionStore.get()).toEqual({
- isReady: true,
- selection: {
- datetime: {
- period: '14d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- },
- });
- });
- it('resets start&end if showAbsolute prop is false', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={organization} showAbsolute={false} />,
- changeQuery(routerContext, {
- start: '2020-05-05T07:26:53.000',
- end: '2020-05-05T09:19:12.000',
- })
- );
- await tick();
- expect(GlobalSelectionStore.get()).toEqual({
- isReady: true,
- selection: {
- datetime: {
- period: '14d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- },
- });
- });
- /**
- * I don't think this test is really applicable anymore
- */
- it('does not update store if url params have not changed', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={organization} />,
- changeQuery(routerContext, {
- statsPeriod: '7d',
- })
- );
- [
- globalActions.updateDateTime,
- globalActions.updateProjects,
- globalActions.updateEnvironments,
- ].forEach(mock => mock.mockClear());
- wrapper.setContext(
- changeQuery(routerContext, {
- statsPeriod: '7d',
- }).context
- );
- await tick();
- wrapper.update();
- expect(globalActions.updateDateTime).not.toHaveBeenCalled();
- expect(globalActions.updateProjects).not.toHaveBeenCalled();
- expect(globalActions.updateEnvironments).not.toHaveBeenCalled();
- expect(GlobalSelectionStore.get()).toEqual({
- isReady: true,
- selection: {
- datetime: {
- period: '7d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- },
- });
- });
- it('loads from local storage when no URL parameters', async function () {
- getItem.mockImplementation(() =>
- JSON.stringify({projects: [3], environments: ['staging']})
- );
- const initializationObj = initializeOrg({
- organization: {
- features: ['global-views'],
- },
- router: {
- // we need this to be set to make sure org in context is same as
- // current org in URL
- params: {orgId: 'org-slug'},
- },
- });
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- await tick(); // reflux tick
- expect(GlobalSelectionStore.get().selection.projects).toEqual([3]);
- // Since these are coming from URL, there should be no changes and
- // router does not need to be called
- expect(initializationObj.router.replace).toHaveBeenLastCalledWith(
- expect.objectContaining({
- query: {
- environment: ['staging'],
- project: [3],
- },
- })
- );
- });
- it('does not load from local storage when there are URL params', async function () {
- getItem.mockImplementation(() =>
- JSON.stringify({projects: [3], environments: ['staging']})
- );
- const initializationObj = initializeOrg({
- organization: {
- features: ['global-views'],
- },
- router: {
- // we need this to be set to make sure org in context is same as
- // current org in URL
- params: {orgId: 'org-slug'},
- location: {query: {project: [1, 2]}},
- },
- });
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- await tick(); // reflux tick
- expect(GlobalSelectionStore.get().selection.projects).toEqual([1, 2]);
- // Since these are coming from URL, there should be no changes and
- // router does not need to be called
- expect(initializationObj.router.replace).not.toHaveBeenCalled();
- });
- it('updates store when there are query params in URL', async function () {
- const initializationObj = initializeOrg({
- organization: {
- features: ['global-views'],
- },
- router: {
- // we need this to be set to make sure org in context is same as
- // current org in URL
- params: {orgId: 'org-slug'},
- location: {query: {project: [1, 2]}},
- },
- });
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- await tick(); // reflux tick
- expect(GlobalSelectionStore.get().selection.projects).toEqual([1, 2]);
- // Since these are coming from URL, there should be no changes and
- // router does not need to be called
- expect(initializationObj.router.replace).not.toHaveBeenCalled();
- });
- it('updates store with default values when there are no query params in URL', async function () {
- const initializationObj = initializeOrg({
- organization: {
- features: ['global-views'],
- },
- router: {
- // we need this to be set to make sure org in context is same as
- // current org in URL
- params: {orgId: 'org-slug'},
- location: {query: {}},
- },
- });
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- // Router does not update because params have not changed
- expect(initializationObj.router.replace).not.toHaveBeenCalled();
- });
- /**
- * GSH: (no global-views)
- * - mounts with no state from router
- * - params org id === org.slug
- *
- * - updateProjects should not be called (enforceSingleProject should not be
- * called)
- *
- * - componentDidUpdate with loadingProjects === true, and pass in list of
- * projects (via projects store)
- *
- * - enforceProject should be called and updateProjects() called with the new
- * project
- * - variation:
- * - params.orgId !== org.slug (e.g. just switched orgs)
- *
- * When switching orgs when not in Issues view, the issues view gets rendered
- * with params.orgId !== org.slug
- *
- * Global selection header gets unmounted and mounted, and in this case
- * nothing should be done until it gets updated and params.orgId === org.slug
- *
- * Separate issue:
- *
- * IssuesList ("child view") renders before a single project is enforced,
- * will require refactoring views so that they depend on GSH enforcing a
- * single project first IF they don't have required feature (and no project id
- * in URL).
- */
- describe('Single project selection mode', function () {
- it('does not do anything while organization is switching in single project', async function () {
- const initialData = initializeOrg({
- organization: {slug: 'old-org-slug'},
- router: {
- // we need this to be set to make sure org in context is same as
- // current org in URL
- params: {orgId: 'org-slug'},
- location: {query: {project: [1]}},
- },
- });
- ProjectsStore.isLoading.mockRestore();
- ProjectsStore.getAll.mockRestore();
- MockApiClient.addMockResponse({
- url: '/organizations/old-org-slug/projects/',
- body: [],
- });
- // This can happen when you switch organization so params.orgId !== the
- // current org in context In this case params.orgId = 'org-slug'
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initialData.organization} />,
- initialData.routerContext
- );
- expect(globalActions.updateProjects).not.toHaveBeenCalled();
- const updatedOrganization = {
- ...organization,
- slug: 'org-slug',
- features: [],
- projects: [TestStubs.Project({id: '123', slug: 'org-slug-project1'})],
- };
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/',
- body: updatedOrganization,
- });
- // Eventually OrganizationContext will fetch org details for `org-slug`
- // and update `organization` prop emulate fetchOrganizationDetails
- OrganizationActions.update(updatedOrganization);
- wrapper.setContext({
- organization: updatedOrganization,
- location: {query: {}},
- router: {
- ...initialData.router,
- location: {query: {}},
- },
- });
- wrapper.setProps({organization: updatedOrganization});
- ProjectsStore.loadInitialData(updatedOrganization.projects);
- expect(initialData.router.replace).toHaveBeenLastCalledWith(
- expect.objectContaining({
- query: {environment: [], project: [123]},
- })
- );
- });
- it('selects first project if more than one is requested', function () {
- const initializationObj = initializeOrg({
- router: {
- // we need this to be set to make sure org in context is same as
- // current org in URL
- params: {orgId: 'org-slug'},
- location: {query: {project: [1, 2]}},
- },
- });
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- expect(initializationObj.router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {environment: [], project: [1]},
- })
- );
- });
- it('selects first project if none (i.e. all) is requested', async function () {
- const project = TestStubs.Project({id: '3'});
- const org = TestStubs.Organization({projects: [project]});
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => org.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- const initializationObj = initializeOrg({
- organization: org,
- router: {
- params: {orgId: 'org-slug'},
- location: {query: {}},
- },
- });
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- expect(initializationObj.router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {environment: [], project: [3]},
- })
- );
- });
- });
- describe('forceProject selection mode', function () {
- beforeEach(function () {
- MockApiClient.addMockResponse({
- url: '/organizations/org-slug/projects/',
- body: [],
- });
- const initialData = initializeOrg({
- organization: {features: ['global-views']},
- projects: [
- {id: 1, slug: 'staging-project', environments: ['staging']},
- {id: 2, slug: 'prod-project', environments: ['prod']},
- ],
- router: {
- location: {query: {}},
- },
- });
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- shouldForceProject
- forceProject={initialData.projects[0]}
- showIssueStreamLink
- />,
- initialData.routerContext
- );
- });
- it('renders a back button to the forced project', function () {
- const back = wrapper.find('BackButtonWrapper');
- expect(back).toHaveLength(1);
- });
- it('renders only environments from the forced project', async function () {
- await wrapper.find('MultipleEnvironmentSelector HeaderItem').simulate('click');
- await wrapper.update();
- const items = wrapper.find('MultipleEnvironmentSelector EnvironmentSelectorItem');
- expect(items.length).toEqual(1);
- expect(items.at(0).text()).toBe('staging');
- });
- });
- describe('without global-views (multi-project feature)', function () {
- describe('without existing URL params', function () {
- const initialData = initializeOrg({
- projects: [
- {id: 0, slug: 'random project', isMember: true},
- {id: 1, slug: 'staging-project', environments: ['staging']},
- {id: 2, slug: 'prod-project', environments: ['prod']},
- ],
- router: {
- location: {query: {}},
- params: {orgId: 'org-slug'},
- },
- });
- const createWrapper = props => {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- params={{orgId: initialData.organization.slug}}
- organization={initialData.organization}
- {...props}
- />,
- initialData.routerContext
- );
- return wrapper;
- };
- beforeEach(function () {
- jest
- .spyOn(ProjectsStore, 'getAll')
- .mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- initialData.router.push.mockClear();
- initialData.router.replace.mockClear();
- });
- it('uses first project in org projects when mounting', async function () {
- createWrapper();
- await tick();
- wrapper.update();
- expect(initialData.router.replace).toHaveBeenLastCalledWith({
- pathname: undefined,
- query: {environment: [], project: [0]},
- });
- });
- it('appends projectId to URL when `forceProject` becomes available (async)', async function () {
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => []);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => true);
- // forceProject generally starts undefined
- createWrapper({shouldForceProject: true});
- // load the projects
- jest
- .spyOn(ProjectsStore, 'getAll')
- .mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- wrapper.setProps({
- forceProject: initialData.projects[1],
- });
- // Force the withProjects HoC to re-render
- ProjectsStore.trigger();
- wrapper.update();
- expect(initialData.router.replace).toHaveBeenLastCalledWith({
- pathname: undefined,
- query: {environment: [], project: [1]},
- });
- expect(initialData.router.replace).toHaveBeenCalledTimes(1);
- });
- it('does not append projectId to URL when `forceProject` becomes available but project id already exists in URL', async function () {
- // forceProject generally starts undefined
- createWrapper({shouldForceProject: true});
- wrapper.setContext({
- router: {
- ...initialData.router,
- location: {
- ...initialData.router.location,
- query: {
- project: 321,
- },
- },
- },
- });
- wrapper.setProps({
- forceProject: initialData.projects[1],
- });
- wrapper.update();
- expect(initialData.router.replace).not.toHaveBeenCalled();
- });
- it('appends projectId to URL when mounted with `forceProject`', async function () {
- // forceProject generally starts undefined
- createWrapper({
- shouldForceProject: true,
- forceProject: initialData.projects[1],
- });
- wrapper.update();
- expect(initialData.router.replace).toHaveBeenLastCalledWith({
- pathname: undefined,
- query: {environment: [], project: [1]},
- });
- });
- });
- describe('with existing URL params', function () {
- const initialData = initializeOrg({
- projects: [
- {id: 0, slug: 'random project', isMember: true},
- {id: 1, slug: 'staging-project', environments: ['staging']},
- {id: 2, slug: 'prod-project', environments: ['prod']},
- ],
- router: {
- location: {query: {statsPeriod: '90d'}},
- params: {orgId: 'org-slug'},
- },
- });
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
- const createWrapper = props => {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- params={{orgId: initialData.organization.slug}}
- organization={initialData.organization}
- {...props}
- />,
- initialData.routerContext
- );
- return wrapper;
- };
- beforeEach(function () {
- initialData.router.push.mockClear();
- initialData.router.replace.mockClear();
- });
- it('appends projectId to URL when mounted with `forceProject`', async function () {
- // forceProject generally starts undefined
- createWrapper({
- shouldForceProject: true,
- forceProject: initialData.projects[1],
- });
- wrapper.update();
- expect(initialData.router.replace).toHaveBeenLastCalledWith({
- pathname: undefined,
- query: {environment: [], project: [1], statsPeriod: '90d'},
- });
- });
- });
- });
- describe('with global-views (multi-project feature)', function () {
- describe('without existing URL params', function () {
- const initialData = initializeOrg({
- organization: {features: ['global-views']},
- projects: [
- {id: 0, slug: 'random project', isMember: true},
- {id: 1, slug: 'staging-project', environments: ['staging']},
- {id: 2, slug: 'prod-project', environments: ['prod']},
- ],
- router: {
- location: {query: {}},
- params: {orgId: 'org-slug'},
- },
- });
- const createWrapper = (props, ctx) => {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- params={{orgId: initialData.organization.slug}}
- organization={initialData.organization}
- {...props}
- />,
- {
- ...initialData.routerContext,
- ...ctx,
- }
- );
- return wrapper;
- };
- beforeEach(function () {
- jest
- .spyOn(ProjectsStore, 'getAll')
- .mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- initialData.router.push.mockClear();
- initialData.router.replace.mockClear();
- });
- it('does not use first project in org projects when mounting (and without localStorage data)', async function () {
- createWrapper();
- await tick();
- wrapper.update();
- expect(initialData.router.replace).not.toHaveBeenCalled();
- });
- it('does not append projectId to URL when `loadingProjects` changes and finishes loading', async function () {
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => []);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => true);
- createWrapper();
- // load the projects
- jest
- .spyOn(ProjectsStore, 'getAll')
- .mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- wrapper.setProps({
- forceProject: initialData.projects[1],
- });
- // Force the withProjects HoC to re-render
- ProjectsStore.trigger();
- expect(initialData.router.replace).not.toHaveBeenCalled();
- });
- it('appends projectId to URL when `forceProject` becomes available (async)', async function () {
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => []);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => true);
- // forceProject generally starts undefined
- createWrapper({shouldForceProject: true});
- // load the projects
- jest
- .spyOn(ProjectsStore, 'getAll')
- .mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- wrapper.setProps({
- forceProject: initialData.projects[1],
- });
- // Force the withProjects HoC to re-render
- ProjectsStore.trigger();
- expect(initialData.router.replace).toHaveBeenLastCalledWith({
- pathname: undefined,
- query: {environment: [], project: [1]},
- });
- expect(initialData.router.replace).toHaveBeenCalledTimes(1);
- });
- it('does not append projectId to URL when `forceProject` becomes available but project id already exists in URL', async function () {
- // forceProject generally starts undefined
- createWrapper(
- {shouldForceProject: true},
- changeQuery(initialData.routerContext, {project: 321})
- );
- await tick();
- wrapper.setProps({
- forceProject: initialData.projects[1],
- });
- wrapper.update();
- expect(initialData.router.replace).not.toHaveBeenCalled();
- });
- });
- });
- describe('projects list', function () {
- let memberProject, nonMemberProject, initialData;
- beforeEach(function () {
- memberProject = TestStubs.Project({id: '3', isMember: true});
- nonMemberProject = TestStubs.Project({id: '4', isMember: false});
- initialData = initializeOrg({
- projects: [memberProject, nonMemberProject],
- router: {
- location: {query: {}},
- params: {
- orgId: 'org-slug',
- },
- },
- });
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initialData.organization} />,
- initialData.routerContext
- );
- });
- it('gets member projects', function () {
- expect(wrapper.find('MultipleProjectSelector').prop('projects')).toEqual([
- memberProject,
- ]);
- });
- it('gets all projects if superuser', function () {
- ConfigStore.config = {
- user: {
- isSuperuser: true,
- },
- };
- wrapper = mountWithTheme(
- <GlobalSelectionHeader organization={initialData.organization} />,
- initialData.routerContext
- );
- expect(wrapper.find('MultipleProjectSelector').prop('projects')).toEqual([
- memberProject,
- ]);
- expect(wrapper.find('MultipleProjectSelector').prop('nonMemberProjects')).toEqual([
- nonMemberProject,
- ]);
- });
- it('shows "My Projects" button', async function () {
- initialData.organization.features.push('global-views');
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- initialData.routerContext
- );
- await tick();
- wrapper.update();
- // open the project menu.
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- const projectSelector = wrapper.find('MultipleProjectSelector');
- // Two projects
- expect(projectSelector.find('AutoCompleteItem')).toHaveLength(2);
- // My projects in the footer
- expect(
- projectSelector.find('SelectorFooterControls Button').first().text()
- ).toEqual('View My Projects');
- });
- it('shows "All Projects" button based on features', async function () {
- initialData.organization.features.push('global-views');
- initialData.organization.features.push('open-membership');
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- initialData.routerContext
- );
- await tick();
- wrapper.update();
- // open the project menu.
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- const projectSelector = wrapper.find('MultipleProjectSelector');
- // Two projects
- expect(projectSelector.find('AutoCompleteItem')).toHaveLength(2);
- // All projects in the footer
- expect(
- projectSelector.find('SelectorFooterControls Button').first().text()
- ).toEqual('View All Projects');
- });
- it('shows "All Projects" button based on role', async function () {
- initialData.organization.features.push('global-views');
- initialData.organization.role = 'owner';
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- initialData.routerContext
- );
- await tick();
- wrapper.update();
- // open the project menu.
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- const projectSelector = wrapper.find('MultipleProjectSelector');
- // Two projects
- expect(projectSelector.find('AutoCompleteItem')).toHaveLength(2);
- // All projects in the footer
- expect(
- projectSelector.find('SelectorFooterControls Button').first().text()
- ).toEqual('View All Projects');
- });
- it('shows "My Projects" when "all projects" is selected', async function () {
- initialData.organization.features.push('global-views');
- initialData.organization.role = 'owner';
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- changeQuery(initialData.routerContext, {project: -1})
- );
- await tick();
- wrapper.update();
- // open the project menu.
- wrapper.find('MultipleProjectSelector HeaderItem').simulate('click');
- const projectSelector = wrapper.find('MultipleProjectSelector');
- // My projects in the footer
- expect(
- projectSelector.find('SelectorFooterControls Button').first().text()
- ).toEqual('View My Projects');
- });
- });
- describe('project icons', function () {
- const initialData = initializeOrg({
- organization: {features: ['global-views']},
- projects: [
- {id: 0, slug: 'go', platform: 'go'},
- {id: 1, slug: 'javascript', platform: 'javascript'},
- {id: 2, slug: 'other', platform: 'other'},
- {id: 3, slug: 'php', platform: 'php'},
- {id: 4, slug: 'python', platform: 'python'},
- {id: 5, slug: 'rust', platform: 'rust'},
- {id: 6, slug: 'swift', platform: 'swift'},
- ],
- });
- beforeEach(function () {
- jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => initialData.projects);
- jest.spyOn(ProjectsStore, 'isLoading').mockImplementation(() => false);
- });
- it('shows IconProject when no projects are selected', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- changeQuery(initialData.routerContext, {project: -1})
- );
- await tick();
- wrapper.update();
- const projectSelector = wrapper.find('MultipleProjectSelector');
- expect(projectSelector.find('IconContainer svg').exists()).toBeTruthy();
- expect(projectSelector.find('PlatformIcon').exists()).toBeFalsy();
- expect(projectSelector.find('Content').text()).toEqual('All Projects');
- });
- it('shows PlatformIcon when one project is selected', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- changeQuery(initialData.routerContext, {project: 1})
- );
- await tick();
- wrapper.update();
- const projectSelector = wrapper.find('MultipleProjectSelector');
- expect(projectSelector.find('StyledPlatformIcon').props().platform).toEqual(
- 'javascript'
- );
- expect(projectSelector.find('Content').text()).toEqual('javascript');
- });
- it('shows multiple PlatformIcons when multiple projects are selected, no more than 5', async function () {
- wrapper = mountWithTheme(
- <GlobalSelectionHeader
- organization={initialData.organization}
- projects={initialData.projects}
- />,
- initialData.routerContext
- );
- await tick();
- wrapper.update();
- // select 6 projects
- const headerItem = wrapper.find('MultipleProjectSelector HeaderItem');
- headerItem.simulate('click');
- wrapper
- .find('MultipleProjectSelector CheckboxFancy')
- .forEach(project => project.simulate('click'));
- headerItem.simulate('click');
- await tick();
- wrapper.update();
- // assert title and icons
- const title = wrapper.find('MultipleProjectSelector Content');
- const icons = wrapper.find('MultipleProjectSelector StyledPlatformIcon');
- expect(title.text()).toBe('javascript, other, php, python, rust, swift');
- expect(icons.length).toBe(5);
- expect(icons.at(3).props().platform).toBe('rust');
- expect(icons.at(4).props().platform).toBe('swift');
- });
- });
- });
|