translations_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Translations, type: :graphql do
  4. context 'when fetching translations' do
  5. let(:query) do
  6. <<~QUERY
  7. query translations($locale: String!, $cacheKey: String) {
  8. translations(locale: $locale, cacheKey: $cacheKey) {
  9. isCacheStillValid
  10. cacheKey
  11. translations
  12. }
  13. }
  14. QUERY
  15. end
  16. let(:variables) { { locale: locale, cacheKey: cache_key } }
  17. let(:expected_cache_key) { Translation.where(locale: locale).reorder(updated_at: :desc).take.updated_at.to_s }
  18. before do
  19. gql.execute(query, variables: variables)
  20. end
  21. context 'with a valid locale' do
  22. let(:locale) { 'de-de' }
  23. context 'without a correct cache_key' do
  24. let(:cache_key) { nil }
  25. it 'returns metadata' do
  26. expect(gql.result.data).to include({ 'isCacheStillValid' => false, 'cacheKey' => expected_cache_key })
  27. end
  28. it 'returns translations' do
  29. expect(gql.result.data['translations']).to include({ 'yes' => 'ja' })
  30. end
  31. it 'does not return empty or "untranslated" translations' do
  32. expect(gql.result.data['translations'].select { |k, v| v.empty? || k == v }).to be_empty
  33. end
  34. end
  35. context 'with the correct cache key' do
  36. let(:cache_key) { expected_cache_key }
  37. it 'returns only metadata' do
  38. expect(gql.result.data).to include({ 'isCacheStillValid' => true, 'cacheKey' => nil, 'translations' => nil })
  39. end
  40. end
  41. end
  42. context 'with an invalid locale' do
  43. let(:locale) { 'invalid-locale' }
  44. let(:cache_key) { nil }
  45. it 'returns error type' do
  46. expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
  47. end
  48. it 'returns error message' do
  49. expect(gql.result.error_message).to eq("No translations found for locale #{locale}.")
  50. end
  51. end
  52. end
  53. end