require 'test_helper' module Titlezilla describe Translator do describe '#initialize' do it 'should initialize controller_name' do t = Translator.new('main', 'index') t.controller_name.must_equal 'main' end it 'should initialize action_name' do t = Translator.new('main', 'index') t.action_name.must_equal 'index' end it 'should initialize namespace' do t = Translator.new('admin/main', 'index') t.namespace.must_equal ['admin'] end it 'should initialize from symbols' do t = Translator.new(:'admin/main', :index) t.namespace.must_equal ['admin'] t.controller_name.must_equal 'main' t.action_name.must_equal 'index' end end describe '#title' do it 'should lookup action title' do load_translations({ main: { index: 'Index' } }) t = Translator.new(:main, :index) t.title.must_equal 'Index' end it 'should lookup action title with namespace' do load_translations({ admin: { main: { index: 'Index' } } }) t = Translator.new('admin/main', :index) t.title.must_equal 'Index' end it 'should lookup action title with deep namespace' do load_translations({ admin: { billing: { main: { index: 'Index' } } } }) t = Translator.new('admin/billing/main', :index) t.title.must_equal 'Index' end it 'should lookup mapped actions title' do load_translations({ main: { new: 'New' } }) t = Translator.new(:main, :create) t.title.must_equal 'New' end it 'should allow using context' do load_translations({ main: { new: 'New: %{name}' } }) t = Translator.new(:main, :create, name: 'John') t.title.must_equal 'New: John' end end describe '#application_title' do it 'should lookup application title' do load_translations({ application: 'App' }) t = Translator.new(:main, :index) t.application_title.must_equal 'App' end it 'should lookup application title when namespace provided' do load_translations({ admin: { application: 'App' } }) t = Translator.new('admin/main', :index) t.application_title.must_equal 'App' end it 'should lookup application title when no namespaced app title provided' do load_translations({ application: 'App' }) t = Translator.new('admin/main', :index) t.application_title.must_equal 'App' end end describe '#meta_title' do it 'should return app title if action title not provided' do load_translations({ application: 'App' }) t = Translator.new(:main, :index) t.meta_title.must_equal 'App' end it 'should return joined title' do load_translations({ application: 'App', main: { index: 'Index' } }) t = Translator.new(:main, :index) t.meta_title.must_equal 'Index | App' end it 'should return joined title with custom separator' do load_translations({ application: 'App', main: { index: 'Index' } }) ::Titlezilla.configure { |c| c.separator = ' > ' } t = Translator.new(:main, :index) t.meta_title.must_equal 'Index > App' end it 'should override title' do load_translations({ application: 'App', main: { index: 'Index' } }) t = Translator.new(:main, :index) t.title 'Overrided' t.meta_title.must_equal 'Overrided | App' end end end end