Skip to content

Commit

Permalink
tests: I've tested the frontend and backend of #1994
Browse files Browse the repository at this point in the history
I've added a new jsx which tests the new component and also I've modified some test for not repeat code.
  • Loading branch information
franpb14 authored and julianguyen committed Feb 19, 2024
1 parent 8b165d3 commit 1dfe7cc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
65 changes: 65 additions & 0 deletions client/app/components/Input/__tests__/InputMultiSelect.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// @flow
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { InputMocks } from 'mocks/InputMocks';
import { InputMultiSelect } from 'components/Input/InputMultiSelect';

const {
id, name, ariaLabel, value, options,
} = InputMocks.inputSelectProps;
const someEvent = InputMocks.event;

describe('InputMultiSelect', () => {
beforeEach(() => {
jest.spyOn(window, 'alert');
});

afterEach(() => {
jest.clearAllMocks();
});

it('renders correctly', () => {
render(
<InputMultiSelect
name={name}
id={id}
ariaLabel={ariaLabel}
value={value}
options={options}
onChange={someEvent}
/>,
);
const multiSelect = screen.getByRole('button', { className: 'buttonL' });
expect(multiSelect).toBeInTheDocument();
expect(screen.getAllByRole('checkbox').length).toEqual(2);
});

it('toggles options correctly', () => {
render(
<InputMultiSelect
name={name}
id={id}
ariaLabel={ariaLabel}
options={options}
onChange={someEvent}
/>,
);
// toggle the first value
const multiSelect = screen.getByRole('componentValue', { hidden: true });
const checkbox = screen.getByRole('checkbox', { name: options[0].label });
const checkbox2 = screen.getByRole('checkbox', { name: options[1].label });

userEvent.click(checkbox);
expect(multiSelect.value).toEqual(`${options[0].value}`);

// update the value
userEvent.click(checkbox2);
expect(multiSelect.value).toEqual(`${options[0].value},${options[1].value}`);

// update the value by unchecking
userEvent.click(checkbox);
expect(multiSelect.value).toEqual(`${options[1].value}`);

});
});
23 changes: 22 additions & 1 deletion spec/concerns/collection_page_setup_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,33 @@ class CollectionPageSetupConcernTestController < ApplicationController
end

describe CollectionPageSetupConcernTestController do
let(:user) { create(:user) }
it 'should setup a page' do
user = create(:user)
allow(subject).to receive(:current_user).and_return(user)
allow(subject).to receive(:params).and_return(search: 'a value')
page_collection = subject.page_collection('@categories', 'category')
expect(subject.params[:search]).to eq 'a value'
expect(page_collection).to be_truthy
end

it 'with filters should setup a page' do
allow(subject).to receive(:current_user).and_return(user)
allow(subject).to receive(:params).and_return(filters: '0')
page_collection = subject.page_collection('@moments', 'moment',
{options: 'No viewers',
filters: [{ viewers: [] }]})
expect(subject.params[:filters]).to eq '0'
expect(page_collection).to be_truthy
end

it 'with secret shared enable filter returns the due' do
moment_secret = create(:moment, :with_secret_share, name: 'secret shared enable', user: user )
moment = create(:moment, name: 'moment without secret', user: user )
allow(subject).to receive(:current_user).and_return(user)
allow(subject).to receive(:params).and_return(filters: '0')
page_collection = subject.page_collection('@moments', 'moment',
{options: 'Secret share enabled',
filters: ['secret_share_identifier IS NOT NULL']})
expect(subject.instance_variable_get(:@moments).ids).to eq [moment_secret.id]
end
end
11 changes: 11 additions & 0 deletions spec/helpers/moments_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,15 @@
end
end
end

describe '#hash_for_multiselect' do
subject { controller.hash_for_multiselect }
it 'contains the 6 options' do
expect(subject[:options].size).to eq(6)
end

it 'contains the 6 filters' do
expect(subject[:filters].size).to eq(6)
end
end
end

0 comments on commit 1dfe7cc

Please sign in to comment.