123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import PluginsStore from 'sentry/stores/pluginsStore';
- describe('PluginsStore', function () {
- beforeEach(function () {
- jest.resetAllMocks();
- });
- 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 () {
- const triggerSpy = jest.spyOn(PluginsStore, 'trigger');
- PluginsStore.onFetchAll();
- expect(triggerSpy).toHaveBeenCalledWith({
- loading: true,
- error: null,
- pageLinks: null,
- plugins: [],
- });
- PluginsStore.onFetchAllSuccess(TestStubs.Plugins(), {pageLinks: undefined});
- expect(triggerSpy).toHaveBeenCalledWith({
- loading: false,
- error: null,
- pageLinks: null,
- plugins: TestStubs.Plugins(),
- });
- });
- it('has correct state when error in fetching all plugins', function () {
- const triggerSpy = jest.spyOn(PluginsStore, 'trigger');
- PluginsStore.onFetchAll();
- expect(triggerSpy).toHaveBeenCalledWith({
- loading: true,
- error: null,
- pageLinks: null,
- plugins: [],
- });
- PluginsStore.onFetchAllError({responseJSON: {message: 'Error'}});
- expect(triggerSpy).toHaveBeenCalledWith({
- loading: false,
- error: {responseJSON: {message: 'Error'}},
- pageLinks: null,
- plugins: [],
- });
- });
- it('does not reset loading state on consecutive fetches', function () {
- const triggerSpy = jest.spyOn(PluginsStore, 'trigger');
- PluginsStore.onFetchAll();
- expect(triggerSpy).toHaveBeenCalledWith({
- loading: true,
- error: null,
- pageLinks: null,
- plugins: [],
- });
- PluginsStore.onFetchAllSuccess(TestStubs.Plugins(), {pageLinks: undefined});
- expect(triggerSpy).toHaveBeenCalledWith({
- loading: false,
- error: null,
- pageLinks: null,
- plugins: TestStubs.Plugins(),
- });
- PluginsStore.onFetchAll();
- expect(triggerSpy).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 () {
- PluginsStore.onUpdate('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 () {
- PluginsStore.onUpdate('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 () {
- PluginsStore.onUpdate('amazon-sqs', {name: 'Amazon Sqs'});
- expect(PluginsStore.updating.get('amazon-sqs')).toMatchObject({
- ...plugin,
- id: 'amazon-sqs',
- name: 'Amazon SQS',
- });
- PluginsStore.onUpdateSuccess('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 () {
- PluginsStore.onUpdate('amazon-sqs', {name: 'Amazon Sqs'});
- expect(PluginsStore.getState().plugins[0]).toMatchObject({
- id: 'amazon-sqs',
- name: 'Amazon Sqs',
- });
- PluginsStore.onUpdateError('amazon-sqs', new Error('error'));
- expect(PluginsStore.getState().plugins[0]).toMatchObject({
- id: 'amazon-sqs',
- name: 'Amazon SQS',
- });
- expect(PluginsStore.updating.get('amazon-sqs')).toEqual(undefined);
- });
- });
- });
|