snippets_spec.rb 2.57 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2 3
require 'spec_helper'

describe "Snippets" do
4
  let(:project) { create(:project) }
gitlabhq's avatar
gitlabhq committed
5

Nihad Abbasov's avatar
Nihad Abbasov committed
6
  before do
gitlabhq's avatar
gitlabhq committed
7 8 9 10 11
    login_as :user
    project.add_access(@user, :read, :write)
  end

  describe "GET /snippets" do
Nihad Abbasov's avatar
Nihad Abbasov committed
12
    before do
13 14 15
      @snippet = create(:snippet,
                        author: @user,
                        project: project)
gitlabhq's avatar
gitlabhq committed
16 17 18 19 20 21

      visit project_snippets_path(project)
    end

    subject { page }

22
    it { should have_content(@snippet.title[0..10]) }
gitlabhq's avatar
gitlabhq committed
23 24
    it { should have_content(@snippet.project.name) }

Nihad Abbasov's avatar
Nihad Abbasov committed
25 26
    describe "Destroy" do
      before do
gitlabhq's avatar
gitlabhq committed
27 28 29
        # admin access to remove snippet
        @user.users_projects.destroy_all
        project.add_access(@user, :read, :write, :admin)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
30
        visit edit_project_snippet_path(project, @snippet)
gitlabhq's avatar
gitlabhq committed
31 32
      end

Nihad Abbasov's avatar
Nihad Abbasov committed
33
      it "should remove entry" do
gitlabhq's avatar
gitlabhq committed
34 35 36 37 38 39 40
        expect {
          click_link "destroy_snippet_#{@snippet.id}"
        }.to change { Snippet.count }.by(-1)
      end
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
41 42
  describe "New snippet" do
    before do
gitlabhq's avatar
gitlabhq committed
43 44 45 46
      visit project_snippets_path(project)
      click_link "New Snippet"
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
47
    it "should open new snippet popup" do
gitlabhq's avatar
gitlabhq committed
48 49 50
      page.current_path.should == new_project_snippet_path(project)
    end

51
    describe "fill in", js: true do
gitlabhq's avatar
gitlabhq committed
52
      before do
53 54
        fill_in "snippet_title", with: "login function"
        fill_in "snippet_file_name", with: "test.rb"
55
        page.execute_script("editor.insert('def login; end');")
gitlabhq's avatar
gitlabhq committed
56 57 58 59
      end

      it { expect { click_button "Save" }.to change {Snippet.count}.by(1) }

Nihad Abbasov's avatar
Nihad Abbasov committed
60
      it "should add new snippet to table" do
gitlabhq's avatar
gitlabhq committed
61 62 63 64 65 66 67 68
        click_button "Save"
        page.current_path.should == project_snippet_path(project, Snippet.last)
        page.should have_content "login function"
        page.should have_content "test.rb"
      end
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
69 70
  describe "Edit snippet" do
    before do
71 72 73
      @snippet = create(:snippet,
                        author: @user,
                        project: project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74
      visit project_snippet_path(project, @snippet)
75
      click_link "Edit"
gitlabhq's avatar
gitlabhq committed
76 77
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
78
    it "should open edit page" do
gitlabhq's avatar
gitlabhq committed
79 80 81
      page.current_path.should == edit_project_snippet_path(project, @snippet)
    end

Nihad Abbasov's avatar
Nihad Abbasov committed
82
    describe "fill in" do
gitlabhq's avatar
gitlabhq committed
83
      before do
84 85
        fill_in "snippet_title", with: "login function"
        fill_in "snippet_file_name", with: "test.rb"
gitlabhq's avatar
gitlabhq committed
86 87 88 89
      end

      it { expect { click_button "Save" }.to_not change {Snippet.count} }

Nihad Abbasov's avatar
Nihad Abbasov committed
90
      it "should update snippet fields" do
gitlabhq's avatar
gitlabhq committed
91 92 93 94 95 96 97 98 99
        click_button "Save"

        page.current_path.should == project_snippet_path(project, @snippet)
        page.should have_content "login function"
        page.should have_content "test.rb"
      end
    end
  end
end