123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- import {
- initializeUrlState,
- updateDateTime,
- updateEnvironments,
- updateParams,
- updateParamsWithoutHistory,
- updateProjects,
- } from 'app/actionCreators/globalSelection';
- import GlobalSelectionActions from 'app/actions/globalSelectionActions';
- import localStorage from 'app/utils/localStorage';
- jest.mock('app/utils/localStorage');
- describe('GlobalSelection ActionCreators', function () {
- const organization = TestStubs.Organization();
- beforeEach(function () {
- localStorage.getItem.mockClear();
- jest.spyOn(GlobalSelectionActions, 'updateProjects');
- jest.spyOn(GlobalSelectionActions, 'initializeUrlState').mockImplementation();
- GlobalSelectionActions.updateProjects.mockClear();
- });
- describe('initializeUrlState', function () {
- let router;
- beforeEach(() => {
- router = TestStubs.router();
- });
- it('loads from local storage when no query params', function () {
- const key = `global-selection:${organization.slug}`;
- localStorage.setItem(key, JSON.stringify({environments: [], projects: [1]}));
- initializeUrlState({
- organization,
- queryParams: {},
- router,
- });
- expect(localStorage.getItem).toHaveBeenCalledWith(
- `global-selection:${organization.slug}`
- );
- expect(GlobalSelectionActions.initializeUrlState).toHaveBeenCalledWith(
- expect.objectContaining({
- environments: [],
- projects: [1],
- })
- );
- expect(router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- environment: [],
- project: [1],
- },
- })
- );
- });
- it('does not load from local storage when no query params and `skipLoadLastUsed` is true', function () {
- jest.spyOn(localStorage, 'getItem');
- initializeUrlState({
- organization,
- queryParams: {},
- skipLoadLastUsed: true,
- router,
- });
- expect(localStorage.getItem).not.toHaveBeenCalled();
- });
- it('does not change dates with no query params or defaultSelection', function () {
- initializeUrlState({
- organization,
- queryParams: {
- project: '1',
- },
- router,
- });
- expect(GlobalSelectionActions.initializeUrlState).toHaveBeenCalledWith(
- expect.objectContaining({
- datetime: {
- start: null,
- end: null,
- period: '14d',
- utc: null,
- },
- })
- );
- });
- it('does changes to default dates with defaultSelection and no query params', function () {
- initializeUrlState({
- organization,
- queryParams: {
- project: '1',
- },
- defaultSelection: {
- datetime: {
- period: '3h',
- },
- },
- router,
- });
- expect(GlobalSelectionActions.initializeUrlState).toHaveBeenCalledWith(
- expect.objectContaining({
- datetime: {
- start: null,
- end: null,
- period: '3h',
- utc: null,
- },
- })
- );
- });
- it('uses query params statsPeriod over defaults', function () {
- initializeUrlState({
- organization,
- queryParams: {
- statsPeriod: '1h',
- project: '1',
- },
- defaultSelection: {
- datetime: {
- period: '24h',
- },
- },
- router,
- });
- expect(router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- cursor: undefined,
- project: [1],
- environment: [],
- statsPeriod: '1h',
- },
- })
- );
- });
- it('uses absolute dates over defaults', function () {
- initializeUrlState({
- organization,
- queryParams: {
- start: '2020-03-22T00:53:38',
- end: '2020-04-21T00:53:38',
- project: '1',
- },
- defaultSelection: {
- datetime: {
- period: '24h',
- },
- },
- router,
- });
- expect(router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- cursor: undefined,
- project: [1],
- environment: [],
- start: '2020-03-22T00:53:38',
- end: '2020-04-21T00:53:38',
- },
- })
- );
- });
- it('does not load from local storage when there are query params', function () {
- initializeUrlState({
- organization,
- queryParams: {
- project: '1',
- },
- router,
- });
- expect(localStorage.getItem).not.toHaveBeenCalled();
- expect(GlobalSelectionActions.initializeUrlState).toHaveBeenCalledWith({
- datetime: {
- start: null,
- end: null,
- period: '14d',
- utc: null,
- },
- projects: [1],
- environments: [],
- });
- expect(router.replace).toHaveBeenCalledWith(
- expect.objectContaining({
- query: {
- environment: [],
- project: [1],
- },
- })
- );
- });
- });
- describe('updateProjects()', function () {
- it('updates', function () {
- updateProjects([1, 2]);
- expect(GlobalSelectionActions.updateProjects).toHaveBeenCalledWith(
- [1, 2],
- undefined
- );
- });
- it('does not update invalid projects', function () {
- updateProjects(['1']);
- expect(GlobalSelectionActions.updateProjects).not.toHaveBeenCalled();
- });
- });
- describe('updateEnvironments()', function () {
- it('updates single', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {environment: 'test'},
- },
- });
- updateEnvironments(['new-env'], router);
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {environment: ['new-env']},
- });
- });
- it('updates multiple', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {environment: 'test'},
- },
- });
- updateEnvironments(['new-env', 'another-env'], router);
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {environment: ['new-env', 'another-env']},
- });
- });
- it('removes environment', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {environment: 'test'},
- },
- });
- updateEnvironments(null, router);
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {},
- });
- });
- });
- describe('updateDateTime()', function () {
- it('updates statsPeriod when there is no existing stats period', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {},
- },
- });
- updateDateTime({statsPeriod: '24h'}, router);
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {
- statsPeriod: '24h',
- },
- });
- });
- it('updates statsPeriod when there is an existing stats period', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {statsPeriod: '14d'},
- },
- });
- updateDateTime({statsPeriod: '24h'}, router);
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {
- statsPeriod: '24h',
- },
- });
- });
- it('updates `statsPeriod` when given a new `period`', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {},
- },
- });
- updateDateTime({period: '24h'}, router);
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {
- statsPeriod: '24h',
- },
- });
- });
- it('changes to absolute date', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {statsPeriod: '24h'},
- },
- });
- updateDateTime({start: '2020-03-22T00:53:38', end: '2020-04-21T00:53:38'}, router);
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {
- start: '2020-03-22T00:53:38',
- end: '2020-04-21T00:53:38',
- },
- });
- });
- });
- describe('updateParams()', function () {
- it('updates history when queries are different', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {project: '2'},
- },
- });
-
-
- updateParams(
- {project: [1]},
-
- router
- );
- expect(router.push).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {project: [1]},
- });
- });
- it('does not update history when queries are the same', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {project: '1'},
- },
- });
-
-
- updateParams(
- {project: [1]},
-
- router
- );
- expect(router.push).not.toHaveBeenCalled();
- });
- });
- describe('updateParamsWithoutHistory()', function () {
- it('updates history when queries are different', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {project: '2'},
- },
- });
-
-
- updateParamsWithoutHistory(
- {project: [1]},
-
- router
- );
- expect(router.replace).toHaveBeenCalledWith({
- pathname: '/test/',
- query: {project: [1]},
- });
- });
- it('does not update history when queries are the same', function () {
- const router = TestStubs.router({
- location: {
- pathname: '/test/',
- query: {project: '1'},
- },
- });
-
-
- updateParamsWithoutHistory(
- {project: [1]},
-
- router
- );
- expect(router.replace).not.toHaveBeenCalled();
- });
- });
- });
|