123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import PluginActions from 'sentry/actions/pluginActions';
- import PluginsStore from 'sentry/stores/pluginsStore';
- describe('PluginsStore', function () {
- beforeAll(function () {
- jest.spyOn(PluginsStore, 'trigger');
- });
- beforeEach(function () {
- PluginsStore.trigger.mockReset();
- });
- afterEach(function () {});
- it('has correct initial state', function () {
- PluginsStore.reset();
- expect(PluginsStore.getState()).toEqual({
- loading: true,
- error: null,
- pageLinks: null,
- plugins: [],
- });
- });
- describe('fetchAll', function () {
- beforeEach(function () {
- PluginsStore.reset();
- });
- it('has correct state when all plugins fetched successfully', function () {
- PluginActions.fetchAll.trigger();
- expect(PluginsStore.trigger).toHaveBeenCalledWith({
- loading: true,
- error: null,
- pageLinks: null,
- plugins: [],
- });
- PluginActions.fetchAllSuccess.trigger(TestStubs.Plugins(), {pageLinks: null});
- expect(PluginsStore.trigger).toHaveBeenCalledWith({
- loading: false,
- error: null,
- pageLinks: null,
- plugins: TestStubs.Plugins(),
- });
- });
- it('has correct state when error in fetching all plugins', function () {
- PluginActions.fetchAll.trigger();
- expect(PluginsStore.trigger).toHaveBeenCalledWith({
- loading: true,
- error: null,
- pageLinks: null,
- plugins: [],
- });
- PluginActions.fetchAllError.trigger({responseJSON: {message: 'Error'}});
- expect(PluginsStore.trigger).toHaveBeenCalledWith({
- loading: false,
- error: {responseJSON: {message: 'Error'}},
- pageLinks: null,
- plugins: [],
- });
- });
- it('does not reset loading state on consecutive fetches', function () {
- PluginActions.fetchAll.trigger();
- expect(PluginsStore.trigger).toHaveBeenCalledWith({
- loading: true,
- error: null,
- pageLinks: null,
- plugins: [],
- });
- PluginActions.fetchAllSuccess.trigger(TestStubs.Plugins(), {pageLinks: null});
- expect(PluginsStore.trigger).toHaveBeenCalledWith({
- loading: false,
- error: null,
- pageLinks: null,
- plugins: TestStubs.Plugins(),
- });
- PluginActions.fetchAll.trigger();
- expect(PluginsStore.trigger).toHaveBeenCalledWith({
- loading: false,
- error: null,
- pageLinks: null,
- plugins: TestStubs.Plugins(),
- });
- });
- });
- describe('update', function () {
- const plugin = TestStubs.Plugin();
- beforeEach(function () {
- PluginsStore.reset();
- PluginsStore.plugins = new Map(TestStubs.Plugins().map(p => [p.id, p]));
- });
- it('has optimistic state when updating', function () {
- PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
- const state = PluginsStore.getState();
- expect(state).toMatchObject({
- error: null,
- pageLinks: null,
- });
- expect(state.plugins[0]).toMatchObject({
- ...plugin,
- id: 'amazon-sqs',
- name: 'Amazon Sqs',
- });
- // Doesn't update other plugins plz
- expect(state.plugins[1]).toMatchObject({
- id: 'github',
- name: 'GitHub',
- });
- });
- it('saves old plugin state', function () {
- PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
- const state = PluginsStore.getState();
- expect(state).toMatchObject({
- error: null,
- pageLinks: null,
- });
- expect(PluginsStore.updating.get('amazon-sqs')).toMatchObject({
- ...plugin,
- id: 'amazon-sqs',
- name: 'Amazon SQS',
- });
- });
- it('removes old plugin state on successful update', function () {
- PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
- expect(PluginsStore.updating.get('amazon-sqs')).toMatchObject({
- ...plugin,
- id: 'amazon-sqs',
- name: 'Amazon SQS',
- });
- PluginActions.updateSuccess.trigger('amazon-sqs');
- expect(PluginsStore.getState().plugins[0]).toMatchObject({
- id: 'amazon-sqs',
- name: 'Amazon Sqs',
- });
- expect(PluginsStore.updating.get('amazon-sqs')).toEqual(undefined);
- });
- it('restores old plugin state when update has an error', function () {
- PluginActions.update.trigger('amazon-sqs', {name: 'Amazon Sqs'});
- expect(PluginsStore.getState().plugins[0]).toMatchObject({
- id: 'amazon-sqs',
- name: 'Amazon Sqs',
- });
- PluginActions.updateError.trigger('amazon-sqs');
- expect(PluginsStore.getState().plugins[0]).toMatchObject({
- id: 'amazon-sqs',
- name: 'Amazon SQS',
- });
- expect(PluginsStore.updating.get('amazon-sqs')).toEqual(undefined);
- });
- });
- });
|