Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2016-02-19 02:19:43 +0300
committerRobert Speicher <rspeicher@gmail.com>2016-02-19 06:45:30 +0300
commit8c454b362425228ab14bb4ed5320f6ba2f505679 (patch)
tree764f96718fd1efef5ccc3eba458db63b6f1aa8d7 /spec/models/blob_spec.rb
parentea4d2741a2b574407b0bd387ccd6a8202c014fc5 (diff)
Add a `Blob` model that wraps `Gitlab::Git::Blob`
This allows us to take advantage of Rails' `to_partial_path` to render the correct partial based on the Blob type, rather than cluttering the view with conditionals. It also allows (and will allow in the future) better encapsulation for Blob-related logic which makes sense for our Rails app but might not make as much sense for the core `gitlab_git` library, such as detecting if the blob is an SVG.
Diffstat (limited to 'spec/models/blob_spec.rb')
-rw-r--r--spec/models/blob_spec.rb81
1 files changed, 81 insertions, 0 deletions
diff --git a/spec/models/blob_spec.rb b/spec/models/blob_spec.rb
new file mode 100644
index 00000000000..78e95c8fac5
--- /dev/null
+++ b/spec/models/blob_spec.rb
@@ -0,0 +1,81 @@
+require 'rails_helper'
+
+describe Blob do
+ describe '.decorate' do
+ it 'returns NilClass when given nil' do
+ expect(described_class.decorate(nil)).to be_nil
+ end
+ end
+
+ describe '#svg?' do
+ it 'is falsey when not text' do
+ git_blob = double(text?: false)
+
+ expect(described_class.decorate(git_blob)).not_to be_svg
+ end
+
+ it 'is falsey when no language is detected' do
+ git_blob = double(text?: true, language: nil)
+
+ expect(described_class.decorate(git_blob)).not_to be_svg
+ end
+
+ it' is falsey when language is not SVG' do
+ git_blob = double(text?: true, language: double(name: 'XML'))
+
+ expect(described_class.decorate(git_blob)).not_to be_svg
+ end
+
+ it 'is truthy when language is SVG' do
+ git_blob = double(text?: true, language: double(name: 'SVG'))
+
+ expect(described_class.decorate(git_blob)).to be_svg
+ end
+ end
+
+ describe '#to_partial_path' do
+ def stubbed_blob(overrides = {})
+ overrides.reverse_merge!(
+ image?: false,
+ language: nil,
+ lfs_pointer?: false,
+ svg?: false,
+ text?: false
+ )
+
+ described_class.decorate(double).tap do |blob|
+ allow(blob).to receive_messages(overrides)
+ end
+ end
+
+ it 'handles LFS pointers' do
+ blob = stubbed_blob(lfs_pointer?: true)
+
+ expect(blob.to_partial_path).to eq 'download'
+ end
+
+ it 'handles SVGs' do
+ blob = stubbed_blob(text?: true, svg?: true)
+
+ expect(blob.to_partial_path).to eq 'image'
+ end
+
+ it 'handles images' do
+ blob = stubbed_blob(image?: true)
+
+ expect(blob.to_partial_path).to eq 'image'
+ end
+
+ it 'handles text' do
+ blob = stubbed_blob(text?: true)
+
+ expect(blob.to_partial_path).to eq 'text'
+ end
+
+ it 'defaults to download' do
+ blob = stubbed_blob
+
+ expect(blob.to_partial_path).to eq 'download'
+ end
+ end
+end