123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- import React from 'react';
- import {initializeOrg} from 'app-test/helpers/initializeOrg';
- import {mount} from 'enzyme';
- import GlobalSelectionHeader from 'app/components/organizations/globalSelectionHeader';
- import GlobalSelectionStore from 'app/stores/globalSelectionStore';
- import * as globalActions from 'app/actionCreators/globalSelection';
- import ProjectsStore from 'app/stores/projectsStore';
- import ConfigStore from 'app/stores/configStore';
- const changeQuery = (routerContext, query) => ({
- ...routerContext,
- context: {
- ...routerContext.context,
- router: {
- ...routerContext.context.router,
- location: {
- query,
- },
- },
- },
- });
- describe('GlobalSelectionHeader', function() {
- const {organization, router, routerContext} = initializeOrg({
- organization: TestStubs.Organization({features: ['global-views']}),
- router: {
- location: {query: {}},
- },
- });
- beforeAll(function() {
- jest.spyOn(globalActions, 'updateDateTime');
- jest.spyOn(globalActions, 'updateEnvironments');
- jest.spyOn(globalActions, 'updateProjects');
- });
- beforeEach(function() {
- GlobalSelectionStore.reset();
- [
- globalActions.updateDateTime,
- globalActions.updateProjects,
- globalActions.updateEnvironments,
- router.push,
- router.replace,
- ].forEach(mock => mock.mockClear());
- });
- it('does not update router if there is custom routing', function() {
- mount(
- <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() {
- mount(<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() {
- mount(<GlobalSelectionHeader organization={organization} />, routerContext);
- expect(router.replace).not.toHaveBeenCalled();
- });
- it('only updates GlobalSelection store when mounted with query params', async function() {
- mount(
- <GlobalSelectionHeader organization={organization} />,
- changeQuery(routerContext, {
- statsPeriod: '7d',
- })
- );
- expect(router.push).not.toHaveBeenCalled();
- expect(globalActions.updateDateTime).toHaveBeenCalledWith({
- period: '7d',
- utc: null,
- start: null,
- end: null,
- });
- expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
- expect(globalActions.updateEnvironments).toHaveBeenCalledWith([]);
- await tick();
- expect(GlobalSelectionStore.get()).toEqual({
- datetime: {
- period: '7d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- });
- });
- it('updates GlobalSelection store when re-rendered with different query params', async function() {
- const wrapper = mount(
- <GlobalSelectionHeader organization={organization} />,
- changeQuery(routerContext, {
- statsPeriod: '7d',
- })
- );
- wrapper.setContext(
- changeQuery(routerContext, {
- statsPeriod: '21d',
- }).context
- );
- await tick();
- wrapper.update();
- expect(globalActions.updateDateTime).toHaveBeenCalledWith({
- period: '21d',
- utc: null,
- start: null,
- end: null,
- });
- expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
- expect(globalActions.updateEnvironments).toHaveBeenCalledWith([]);
- expect(GlobalSelectionStore.get()).toEqual({
- datetime: {
- period: '21d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- });
- });
- it('updates GlobalSelection store with default period', async function() {
- mount(
- <GlobalSelectionHeader organization={organization} />,
- changeQuery(routerContext, {
- environment: 'prod',
- })
- );
- expect(router.push).not.toHaveBeenCalled();
- expect(globalActions.updateDateTime).toHaveBeenCalledWith({
- period: '14d',
- utc: null,
- start: null,
- end: null,
- });
- expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
- expect(globalActions.updateEnvironments).toHaveBeenCalledWith(['prod']);
- await tick();
- expect(GlobalSelectionStore.get()).toEqual({
- datetime: {
- period: '14d',
- utc: null,
- start: null,
- end: null,
- },
- environments: ['prod'],
- projects: [],
- });
- });
- it('updates GlobalSelection store with empty date selections', async function() {
- const wrapper = mount(
- <GlobalSelectionHeader organization={organization} />,
- changeQuery(routerContext, {
- statsPeriod: '7d',
- })
- );
- wrapper.setContext(
- changeQuery(routerContext, {
- statsPeriod: null,
- }).context
- );
- await tick();
- wrapper.update();
- expect(globalActions.updateDateTime).toHaveBeenCalledWith({
- period: null,
- utc: null,
- start: null,
- end: null,
- });
- expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
- expect(globalActions.updateEnvironments).toHaveBeenCalledWith([]);
- expect(GlobalSelectionStore.get()).toEqual({
- datetime: {
- period: null,
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- });
- });
- it('does not update store if url params have not changed', async function() {
- const wrapper = mount(
- <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({
- datetime: {
- period: '7d',
- utc: null,
- start: null,
- end: null,
- },
- environments: [],
- projects: [],
- });
- });
- describe('Single project selection mode', function() {
- it('selects first project if more than one is requested', function() {
- const initializationObj = initializeOrg({
- router: {
- params: {orgId: 'org-slug'}, // we need this to be set to make sure org in context is same as current org in URL
- location: {query: {project: [1, 2]}},
- },
- });
- mount(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- expect(globalActions.updateProjects).toHaveBeenCalledWith([1]);
- });
- it('selects first project if none (i.e. all) is requested', function() {
- const project = TestStubs.Project({id: '3'});
- const org = TestStubs.Organization({projects: [project]});
- ProjectsStore.loadInitialData(org.projects);
- const initializationObj = initializeOrg({
- organization: org,
- router: {
- params: {orgId: 'org-slug'},
- location: {query: {}},
- },
- });
- mount(
- <GlobalSelectionHeader organization={initializationObj.organization} />,
- initializationObj.routerContext
- );
- expect(globalActions.updateProjects).toHaveBeenCalledWith([3]);
- });
- });
- describe('forceProject selection mode', function() {
- 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: {}},
- },
- });
- const wrapper = mount(
- <GlobalSelectionHeader
- organization={initialData.organization}
- forceProject={initialData.organization.projects[0]}
- />,
- 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('projects list', function() {
- let wrapper, memberProject, nonMemberProject, initialData;
- beforeEach(function() {
- memberProject = TestStubs.Project({id: '3', isMember: true});
- nonMemberProject = TestStubs.Project({id: '4', isMember: false});
- const org = TestStubs.Organization({projects: [memberProject, nonMemberProject]});
- ProjectsStore.loadInitialData(org.projects);
- initialData = initializeOrg({
- organization: org,
- router: {
- location: {query: {}},
- },
- });
- wrapper = mount(
- <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 = mount(
- <GlobalSelectionHeader organization={initialData.organization} />,
- initialData.routerContext
- );
- expect(wrapper.find('MultipleProjectSelector').prop('projects')).toEqual([
- memberProject,
- ]);
- expect(wrapper.find('MultipleProjectSelector').prop('nonMemberProjects')).toEqual([
- nonMemberProject,
- ]);
- });
- });
- });
|