state-integration.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import { fromJS, OrderedMap } from "immutable"
  2. import {
  3. selectedServer,
  4. serverVariableValue,
  5. serverVariables,
  6. serverEffectiveValue
  7. } from "corePlugins/oas3/selectors"
  8. import reducers from "corePlugins/oas3/reducers"
  9. import {
  10. setSelectedServer,
  11. setServerVariableValue,
  12. } from "corePlugins/oas3/actions"
  13. describe("OAS3 plugin - state", function() {
  14. describe("action + reducer + selector integration", function() {
  15. describe("selectedServer", function() {
  16. it("should set and get a global selectedServer", function() {
  17. const state = new OrderedMap()
  18. const system = {
  19. // needed to handle `onlyOAS3` wrapper
  20. getSystem() {
  21. return {
  22. specSelectors: {
  23. specJson: () => {
  24. return fromJS({ openapi: "3.0.0" })
  25. }
  26. }
  27. }
  28. }
  29. }
  30. // Create the action
  31. const action = setSelectedServer("http://google.com")
  32. // Collect the new state
  33. const newState = reducers["oas3_set_servers"](state, action)
  34. // Get the value with the selector
  35. const res = selectedServer(newState)(system)
  36. expect(res).toEqual("http://google.com")
  37. })
  38. it("should set and get a namespaced selectedServer", function() {
  39. const state = fromJS({
  40. selectedServer: "http://yahoo.com"
  41. })
  42. const system = {
  43. // needed to handle `onlyOAS3` wrapper
  44. getSystem() {
  45. return {
  46. specSelectors: {
  47. specJson: () => {
  48. return fromJS({ openapi: "3.0.0" })
  49. }
  50. }
  51. }
  52. }
  53. }
  54. // Create the action
  55. const action = setSelectedServer("http://google.com", "myOperation")
  56. // Collect the new state
  57. const newState = reducers["oas3_set_servers"](state, action)
  58. // Get the value with the selector
  59. const res = selectedServer(newState, "myOperation")(system)
  60. // Get the global selected server
  61. const globalRes = selectedServer(newState)(system)
  62. expect(res).toEqual("http://google.com")
  63. expect(globalRes).toEqual("http://yahoo.com")
  64. })
  65. })
  66. describe("serverVariableValue", function() {
  67. it("should set and get a global serverVariableValue", function() {
  68. const state = new OrderedMap()
  69. const system = {
  70. // needed to handle `onlyOAS3` wrapper
  71. getSystem() {
  72. return {
  73. specSelectors: {
  74. specJson: () => {
  75. return fromJS({ openapi: "3.0.0" })
  76. }
  77. }
  78. }
  79. }
  80. }
  81. // Create the action
  82. const action = setServerVariableValue({
  83. server: "google.com",
  84. key: "foo",
  85. val: "bar"
  86. })
  87. // Collect the new state
  88. const newState = reducers["oas3_set_server_variable_value"](state, action)
  89. // Get the value with the selector
  90. const res = serverVariableValue(newState, "google.com", "foo")(system)
  91. expect(res).toEqual("bar")
  92. })
  93. it("should set and get a namespaced serverVariableValue", function() {
  94. const state = fromJS({
  95. serverVariableValues: {
  96. "google.com": {
  97. foo: "123"
  98. }
  99. }
  100. })
  101. const system = {
  102. // needed to handle `onlyOAS3` wrapper
  103. getSystem() {
  104. return {
  105. specSelectors: {
  106. specJson: () => {
  107. return fromJS({ openapi: "3.0.0" })
  108. }
  109. }
  110. }
  111. }
  112. }
  113. // Create the action
  114. const action = setServerVariableValue({
  115. namespace: "myOperation",
  116. server: "google.com",
  117. key: "foo",
  118. val: "bar"
  119. })
  120. // Collect the new state
  121. const newState = reducers["oas3_set_server_variable_value"](state, action)
  122. // Get the value with the selector
  123. const res = serverVariableValue(newState, {
  124. namespace: "myOperation",
  125. server: "google.com"
  126. }, "foo")(system)
  127. // Get the global value, to cross-check
  128. const globalRes = serverVariableValue(newState, {
  129. server: "google.com"
  130. }, "foo")(system)
  131. expect(res).toEqual("bar")
  132. expect(globalRes).toEqual("123")
  133. })
  134. })
  135. describe("serverVariables", function() {
  136. it("should set and get global serverVariables", function() {
  137. const state = new OrderedMap()
  138. const system = {
  139. // needed to handle `onlyOAS3` wrapper
  140. getSystem() {
  141. return {
  142. specSelectors: {
  143. specJson: () => {
  144. return fromJS({ openapi: "3.0.0" })
  145. }
  146. }
  147. }
  148. }
  149. }
  150. // Create the action
  151. const action = setServerVariableValue({
  152. server: "google.com",
  153. key: "foo",
  154. val: "bar"
  155. })
  156. // Collect the new state
  157. const newState = reducers["oas3_set_server_variable_value"](state, action)
  158. // Get the value with the selector
  159. const res = serverVariables(newState, "google.com", "foo")(system)
  160. expect(res.toJS()).toEqual({
  161. foo: "bar"
  162. })
  163. })
  164. it("should set and get namespaced serverVariables", function() {
  165. const state = fromJS({
  166. serverVariableValues: {
  167. "google.com": {
  168. foo: "123"
  169. }
  170. }
  171. })
  172. const system = {
  173. // needed to handle `onlyOAS3` wrapper
  174. getSystem() {
  175. return {
  176. specSelectors: {
  177. specJson: () => {
  178. return fromJS({ openapi: "3.0.0" })
  179. }
  180. }
  181. }
  182. }
  183. }
  184. // Create the action
  185. const action = setServerVariableValue({
  186. namespace: "myOperation",
  187. server: "google.com",
  188. key: "foo",
  189. val: "bar"
  190. })
  191. // Collect the new state
  192. const newState = reducers["oas3_set_server_variable_value"](state, action)
  193. // Get the value with the selector
  194. const res = serverVariables(newState, {
  195. namespace: "myOperation",
  196. server: "google.com"
  197. }, "foo")(system)
  198. // Get the global value, to cross-check
  199. const globalRes = serverVariables(newState, {
  200. server: "google.com"
  201. }, "foo")(system)
  202. expect(res.toJS()).toEqual({
  203. foo: "bar"
  204. })
  205. expect(globalRes.toJS()).toEqual({
  206. foo: "123"
  207. })
  208. })
  209. })
  210. describe("serverEffectiveValue", function() {
  211. it("should set variable values and compute a URL for a namespaced server", function() {
  212. const state = fromJS({
  213. serverVariableValues: {
  214. "google.com/{foo}": {
  215. foo: "123"
  216. }
  217. }
  218. })
  219. const system = {
  220. // needed to handle `onlyOAS3` wrapper
  221. getSystem() {
  222. return {
  223. specSelectors: {
  224. specJson: () => {
  225. return fromJS({ openapi: "3.0.0" })
  226. }
  227. }
  228. }
  229. }
  230. }
  231. // Create the action
  232. const action = setServerVariableValue({
  233. namespace: "myOperation",
  234. server: "google.com/{foo}",
  235. key: "foo",
  236. val: "bar"
  237. })
  238. // Collect the new state
  239. const newState = reducers["oas3_set_server_variable_value"](state, action)
  240. // Get the value with the selector
  241. const res = serverEffectiveValue(newState, {
  242. namespace: "myOperation",
  243. server: "google.com/{foo}"
  244. })(system)
  245. // Get the global value, to cross-check
  246. const globalRes = serverEffectiveValue(newState, {
  247. server: "google.com/{foo}"
  248. })(system)
  249. expect(res).toEqual("google.com/bar")
  250. expect(globalRes).toEqual("google.com/123")
  251. })
  252. })
  253. })
  254. describe("selectors", function() {
  255. describe("serverEffectiveValue", function() {
  256. it("should compute global serverEffectiveValues", function() {
  257. const state = fromJS({
  258. serverVariableValues: {
  259. "google.com/{foo}/{bar}": {
  260. foo: "123",
  261. bar: "456"
  262. }
  263. }
  264. })
  265. const system = {
  266. // needed to handle `onlyOAS3` wrapper
  267. getSystem() {
  268. return {
  269. specSelectors: {
  270. specJson: () => {
  271. return fromJS({ openapi: "3.0.0" })
  272. }
  273. }
  274. }
  275. }
  276. }
  277. // Get the value with the selector
  278. const res = serverEffectiveValue(state, "google.com/{foo}/{bar}")(system)
  279. expect(res).toEqual("google.com/123/456")
  280. })
  281. it("should handle multiple variable instances", function() {
  282. const state = fromJS({
  283. serverVariableValues: {
  284. "google.com/{foo}/{foo}/{bar}": {
  285. foo: "123",
  286. bar: "456"
  287. }
  288. }
  289. })
  290. const system = {
  291. // needed to handle `onlyOAS3` wrapper
  292. getSystem() {
  293. return {
  294. specSelectors: {
  295. specJson: () => {
  296. return fromJS({ openapi: "3.0.0" })
  297. }
  298. }
  299. }
  300. }
  301. }
  302. // Get the value with the selector
  303. const res = serverEffectiveValue(state, "google.com/{foo}/{foo}/{bar}")(system)
  304. expect(res).toEqual("google.com/123/123/456")
  305. })
  306. })
  307. })
  308. })